Jump to content

User:Quarl/userscript.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Quarl (talk | contribs) at 00:16, 11 January 2006. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// User:Quarl/userscript.js - utilities to help develop user scripts

// - adds "raw" tab to user script pages
// - automatically refreshes the raw version when you save a user script page

// requires addlilink.js, get_page_name.js

// quarl 2006-01-10 initial version

function userscriptMakeUrl(pagename) {
    pagename = pagename.replace(' ','_');
    // DON'T escape() because the URL has to match the URL the user uses,
    // which is probably "User:Quarl/..." instead of "User%3AQuarl/...".
    // TODO: may have to manually do partial escape
    return 'http://en.wikipedia.org/w/index.php?title=' + pagename + '&action=raw&ctype=text/javascript&dontcountme=s';
}

function addTab_UserScriptRaw() {
    var pname = getPname();
    if (pname.match(/[.]js$/)) {
        addTab(userscriptMakeUrl(pname), 'raw', 'ca-raw', "Show raw version of user script");
    }
}

function addTab_UserScriptRefresh() {
    var pname = getPname();
    if (pname.match(/[.]js$/)) {
        addTab('javascript:userscript_DoRefresh()', 'refresh', 'ca-refresh', "Force browser to refresh this script");
    }
}

/*function userscript_AutoRefresh_Load() {
    if (get_query_vars()['refresh'] == 'userscript') {
        userscript_DoRefresh();
    }
    userscript_ModifyPageRefreshOnSave();
}*/

function userscript_DoRefresh() {
    force_refresh(userscriptMakeUrl(getPname()));
}

function force_refresh(url) {
    // We can't do frm.location.reload() right now because frm.location will still be "about:blank", but if we complete this event and start a new one, it works.

    //frm.location.replace(url);
    //frm.location.href = url;
    document.getElementById('userscript_refresher').src = url;
    setTimeout('force_refresh_cont_1()', 0);
}

function force_refresh_cont_1() {
    var frm = window.frames['userscript_refresher'];
    frm.location.reload();
}

/*function userscript_ModifyPageRefreshOnSave() {
    document.editform.action += '&refresh=userscript';
}*/

addOnloadHook(addTab_UserScriptRaw);
addOnloadHook(addTab_UserScriptRefresh);
//addOnloadHook(userscript_AutoRefresh_Load);

// hidden iframe
document.write('<div style="position:absolute;left:0px;top:0px;visibility:hidden;" id="hidden_userscript_iframe_div"><iframe src="about:blank" height="0" width="0" name="userscript_refresher" id="userscript_refresher"></iframe></div>');