User:Tonymetz/Scripts/sandbox-protection.js

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/**
 * @name Sandbox Protection
 * @author tonymetz
 * @description Automatically prepends __NOINDEX__ and {{Userspace draft}} to new or overwritten sandbox subpages.
 * @version 1.0.0
 * @license CC-BY-SA-4.0
 * @link https://en.wikipedia.org/wiki/User:Tonymetz/Scripts/sandbox-protection.js
 */
/* Optimized: Ensures {{Userspace draft}} is in the header of Sandbox subpages */
(function() {
    $('#editform').submit(function() {
        var title = mw.config.get('wgPageName');
        var namespace = mw.config.get('wgNamespaceNumber');
        var editBox = document.getElementById('wpTextbox1');
        if (namespace === 2 && title.toLowerCase().includes('/sandbox/') && editBox) {
            var content = editBox.value;
            var headerZone = content.substring(0, 500); 
            var template = '{{Userspace draft}}';
            var noIndex = '__NOINDEX__';
            var additions = '';
            if (!headerZone.includes(noIndex)) {
                additions += noIndex + '\n';
            }
            if (!headerZone.includes(template)) {
                additions += template + '\n';
            }
            if (additions !== '') {
                editBox.value = additions + content;
            }
        }
    });
})();