Jump to content

User:Quarl/userscript.js: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
«"js" → "(?:js|css)"»
«98 words changed»
Line 1: Line 1:
// User:Quarl/userscript.js - utilities to help develop user scripts
// [[User:Quarl/userscript.js]] - utilities to help develop user scripts and
// style sheets


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


// requires wikipage.js, cookie.js, addlilink.js
// depends: wikipage.js, cookie.js, wikitabs.js


// quarl 2006-01-10 initial version
// quarl 2006-01-10 initial version


var userscript = new Object();
function userscriptMakeUrl(wp) {

// note: wp.qurl is parsed from source and is not the same as blindly escaping the input! String
userscript.rawUrl = function(wp) {
// has to exactly match what the user is using, e.g. "User:Quarl/script", not "User%3AQuarl/script"!
// note: wp.qurl is parsed from source and is not the same as blindly
return wp.qurl + '&action=raw&ctype=text/javascript&dontcountme=s';
// escaping the input! String has to exactly match what the user is
// using, e.g. "User:Quarl/script", not "User%3AQuarl/script"!

if (wp.page.match(/[.]js$/)) {
return wp.qurl + '&action=raw&ctype=text/javascript&dontcountme=s';
} else if (wp.page.match(/[.]css$/)) {
return wp.qurl + '&action=raw&ctype=text/css&dontcountme=s';
} else {
return null;
}
}
}


userscript._load = function() {
function userscript_load() {
var url = userscript.rawUrl(wikipage);
if (!wikiPage.article.match(/[.](?:js|css)$/)) return;
if (!url) return;


addTab(userscriptMakeUrl(wikiPage), 'raw', 'ca-raw', "Show raw version of user script");
addTab(url, 'raw', 'ca-raw', "Show raw version of user script");
addTab('javascript:userscript_DoRefresh()', 'refresh', 'ca-refresh', "Force browser to refresh the raw version of this script");
addTab('javascript:userscript.doRefresh()', 'refresh', 'ca-refresh', "Force browser to refresh the raw version of this script");


userscript_refreshIfChanged();
userscript.refreshIfChanged();
}
}


// Check if the current script has changed since last time we looked at it;
// Check if the current script has changed since last time we looked at it;
// if so, refresh. Use a cookie to check.
// if so, refresh. Use a cookie to check.
userscript.refreshIfChanged = function() {
function userscript_refreshIfChanged() {
// if we're not looking at current revision, don't do anything.
// if we're not looking at current revision, don't do anything.
if (permalinkP) return;
if (permalinkP) return;
Line 44: Line 56:
if (last_oldid != pageOldid) {
if (last_oldid != pageOldid) {
// first time we're looking at the page, or the page has changed. refresh.
// first time we're looking at the page, or the page has changed. refresh.
userscript_DoRefresh();
userscript.doRefresh();
}
}
createCookie(cookie_name, pageOldid, 7, path);
createCookie(cookie_name, pageOldid, 7, path);
}
}


userscript.doRefresh = function() {
function userscript_DoRefresh() {
force_refresh(userscriptMakeUrl(wikiPage));
userscript.forceRefresh(userscript.rawUrl(wikiPage));
}
}


userscript._forceRefresh = function(url) {
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.
// 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.


Line 59: Line 71:
//frm.location.href = url;
//frm.location.href = url;
document.getElementById('userscript_refresher').src = url;
document.getElementById('userscript_refresher').src = url;
setTimeout('force_refresh_cont_1()', 0);
setTimeout(userscript._forceRefresh_1, 0);
}
}


userscript._forceRefresh_1 = function() {
function force_refresh_cont_1() {
var frm = window.frames['userscript_refresher'];
var frm = window.frames['userscript_refresher'];
frm.location.reload();
frm.location.reload();
Line 70: Line 82:
}
}


addOnloadHook(userscript_load);
addOnloadHook(userscript._load);


// hidden iframe
// hidden iframe

Revision as of 11:04, 8 February 2006

// [[User:Quarl/userscript.js]] - utilities to help develop user scripts and
// style sheets

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

// depends: wikipage.js, cookie.js, wikitabs.js

// quarl 2006-01-10 initial version

var userscript = new Object();

userscript.rawUrl = function(wp) {
    // note: wp.qurl is parsed from source and is not the same as blindly
    // escaping the input!  String has to exactly match what the user is
    // using, e.g. "User:Quarl/script", not "User%3AQuarl/script"!

    if (wp.page.match(/[.]js$/)) {
        return wp.qurl + '&action=raw&ctype=text/javascript&dontcountme=s';
    } else if (wp.page.match(/[.]css$/)) {
        return wp.qurl + '&action=raw&ctype=text/css&dontcountme=s';
    } else {
        return null;
    }
}

userscript._load = function() {
    var url = userscript.rawUrl(wikipage);
    if (!url) return;

    addTab(url, 'raw', 'ca-raw', "Show raw version of user script");
    addTab('javascript:userscript.doRefresh()', 'refresh', 'ca-refresh', "Force browser to refresh the raw version of this script");

    userscript.refreshIfChanged();
}

// Check if the current script has changed since last time we looked at it;
// if so, refresh.  Use a cookie to check.
userscript.refreshIfChanged = function() {
    // if we're not looking at current revision, don't do anything.
    if (permalinkP) return;

    // we don't have the oldid for some reason (e.g. we're currently editing)
    if (!pageOldid) return;

    //var cookie_name = 'usl_oldid_' + wikiPage.pageQuoted;
    var cookie_name = 'usl_oldid';

    // by using a different cookie-path we avoid sending tons of cookies for every page,
    // and we don't have to uniquify the cookie name.  Note that if unspecified, the path
    // is equivalent to "/wiki/User:Quarl/" instead of "/wiki/User:Quarl/foo.js", which is
    // just as bad as "/"
    var path = url_getpath(wikiPage.url); // only bother with canonical URL
    last_oldid = readCookie(cookie_name);
    if (last_oldid != pageOldid) {
        // first time we're looking at the page, or the page has changed. refresh.
        userscript.doRefresh();
    }
    createCookie(cookie_name, pageOldid, 7, path);
}

userscript.doRefresh = function() {
    userscript.forceRefresh(userscript.rawUrl(wikiPage));
}

userscript._forceRefresh = function(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(userscript._forceRefresh_1, 0);
}

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

    // status
    document.getElementById('ca-refresh').innerHTML = '<b>refreshed</b>';
}

addOnloadHook(userscript._load);

// hidden iframe
if (false) // debug
document.write('<div style="float:right;" id="hidden_userscript_iframe_div"><iframe src="about:blank" height="80" width="80" name="userscript_refresher" id="userscript_refresher"></iframe></div>');
else
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>');