User:Mwarren us/common.js

// Add an onClick function to the Mobile View link to unset the stopMobileRedirect cookie // (Aug 15: Should not be needed any longer; TheDJ put it in the global Mediawiki:common.js file: // https://en.wikipedia.org/w/index.php?title=MediaWiki%3ACommon.js&diff=507511838&oldid=499562302 ) // alas; the devil is in the details - setting the cookie to false is better than setting it to null.

/**** Bug 38009 - https://bugzilla.wikimedia.org/show_bug.cgi?id=38009 ****/

/* This works too allows me to check the default "Mobile View" link too. /* Adding the handler to a link created by .after */

$('a[href$="toggle_view_mobile"]').after(' '   +'Mobile view (w/ doc.cookie)');

$('a#jq-link').click(function{        document.cookie = 'stopMobileRedirect=false; expires=Thu, 01-Jan-1970 00:00:00 GMT; '                         + 'path=/; domain=.wikipedia.org'; });

/** Fails: Use full set of parameters, but as separate arguments (Arthur's suggestion in Bugzilla) */ $('a#jq-link').after(' '   +'Mobile view (w/ $cookie.)');

$('a#jq-link2').click(function{		$.cookie( 'stopMobileRedirect', false, 'Thu, 01-Jan-1970 00:00:00 GMT','/' ); });

/** Try: this call to $.(cookie) works from firebug; maybe it will work in Safari */ $('a#jq-link2').after(' '   +'Mobile view (w/ $cookie.options)');

$('a#jq-link3').click(function{   $.cookie( 'stopMobileRedirect', 'false', {path: '/', domain: '.wikipedia.org'}); });

/******* More examples of things that work and do not work. Reverse chronological ********/

/***************** Experiments using document.cookie (see below for $.cookie ) *********/

/** This works - hardcode the handler into the "newlink" html string */ /* // Straight html for a working link and click handler var newlink = ' ' +'Mobile view (w/ static fix)';

// Create a link using the above html $('a[href$="toggle_view_mobile"]').after(newlink);

/** This works - leave the default "Mobile View" link and */ /* create a new link using .after and add with a new handler. */ /* $('a[href$="toggle_view_mobile"]').after(' '   +'Mobile view (w/ cookie fix)');

$('a#jq-link').click(function{        document.cookie = 'stopMobileRedirect=false; domain=.wikipedia.org; '                         + 'path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT;'; });

/** This works very well - remove non-working handler, replace with working version */ /* // Remove MediaWiki:common.js' handler; it doesn't work yet. $('a[href$="toggle_view_mobile"]').unbind('click'); // Add a handler that does work $('a[href$="toggle_view_mobile"]').click(function{        document.cookie = 'stopMobileRedirect=false; domain=.wikipedia.org;'                         + 'path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT;' });

/** This works too: removed window from anon function passed to .click */ /* $('a[href$="toggle_view_mobile"]').click(function{        document.cookie = 'stopMobileRedirect=false; domain=.wikipedia.org;'                         + 'path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT;' });

/** This works: set document.cookie using a string */ /* $('a[href$="toggle_view_mobile"]').click(function(window){        document.cookie = 'stopMobileRedirect=false; domain=.wikipedia.org;'                         + 'path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT;' });

/** failed with error "window.document.cookie not defined" from inside of jQuery. window.document.cookie = document.cookie.replace(/stopMobileRedirect\s*=[^;]+;/ig,'stopMobileRedirect=false;');

/** failed - browser still hangs onto true value document.cookie = document.cookie.replace(/stopMobileRedirect\s*=[^;]+;/ig,'stopMobileRedirect=false;');

/** failed - appending cookie text wound up with multiple cookies set :(    document.cookie+='stopMobileRedirect=false; expires=Thu, 01-Jan-1970 00:00:00 GMT; path=/; domain=.wikipedia.org';

/********************* Experiments using $.cookie **********************/

/** failed - use $.cookie with full set of parameters */ /* $('a[href$="toggle_view_mobile"]').click(function{                 $.cookie('stopMobileRedirect','false', {expires: 'Thu, 01-Jan-1970 00:00:00 GMT', path: '/', domain: '.wikipedia.org'});                 });



/** failed - doesn't delete cookie $.cookie('stopMobileRedirect','true',{expires: 'Thu, 01-Jan-1970 00:00:00 GMT'});

/*********** View cookies inline to help debug in iOS *************/ /* $("div#footer").append(' document.cookie '                       + ' '                        + ' ' + printCookies                        + '  ');

function printCookies(w){ var cStr = ""; var dt = "";   // formatting - could be  var dtdd = ""; // formatting - could be <dd> var dd = "</dd>"; // formatting - could be </dd> var ck = new Array; ck = document.cookie.split('; '); cStr += '<dt>' + document.cookie.length + ' bytes</dt>'; cStr += '<dd>Raw cookie:\n' + document.cookie + '</dd>'; cStr += '<dt>Edit cookie:</dt>' // RegExp fix from http://lea.verou.me/2009/12/reading-cookies-the-regular-expression-way/ cStr += '<dd>' + document.cookie.replace(/stopMobileRedirect\s*=(.*?)(?:;|$)/ig,'stopMobileRedirect=false;') + '</dd>'; for(i = 0; i < ck.length; i++){ NmeVal = new Array; NmeVal = ck[i].split('='); if(NmeVal[0]){ cStr += dt + unescape(NmeVal[0]) + dtdd + unescape(NmeVal[1]) + dd; }   }    return cStr; }

// alert(printCookies(window));

// shows the cookie contents at the bottom of the page. /*     $("div#footer").append(' document.cookie '                        +' '                        + toggler + ' ' + printCookies                        + '</dl>  '); importScript('User:Enterprisey/diff-context.js'); // Backlink: User:Enterprisey/diff-context.js