User:Splarka/nosearchsuggest.js

From Wikipedia, the free encyclopedia
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.
/* Cookie-based search suggest disable test for anon users, version [0.0.1]

Sets a cookie via URL parameter, examples:
  disable search suggest: http://en.wikipedia.org/wiki/Main_Page?disableSearchSuggest=true
 reenable search suggest: http://en.wikipedia.org/wiki/Main_Page?enableSearchSuggest=true

Would need to be placed in the site-wide JS (possibly conditional on wgUserName) and links
to the enable/disable provided (possibly on Special:Search).

Hacky and probably unneeded if https://bugzilla.wikimedia.org/show_bug.cgi?id=13848 is done.
*/

if(getCookie('noSearchSuggest') == 'true') { 
  //disable init of search suggest (hacky)
  function os_initHandlers() {}
}
if(queryString('disableSearchSuggest')) setCookie('noSearchSuggest','true')
if(queryString('enableSearchSuggest')) setCookie('noSearchSuggest','false')

function setCookie(cookieName, cookieValue) {
 var today = new Date();
 var expire = new Date();
 var nDays = 365;
 expire.setTime(today.getTime() + (3600000 * 24 * nDays));
 document.cookie = cookieName + "=" + escape(cookieValue) + ";path=/"  + ";expires="+expire.toGMTString();
}

function getCookie(cookieName) {
  var start = document.cookie.indexOf(cookieName + "=");
  if (start == -1) return "";
  var len = start + cookieName.length + 1;
  if ((!start) && (cookieName != document.cookie.substring(0,cookieName.length))) {
    return '';
  }
  var end = document.cookie.indexOf(";",len);
  if (end == -1) end = document.cookie.length;
  return unescape(document.cookie.substring(len, end));
}

function queryString(p) {
  var re = RegExp('[&?]' + p + '=([^&]*)');
  var matches;
  if (matches = re.exec(document.location)) {
    try { 
      return decodeURI(matches[1]);
    } catch (e) {
    }
  }
  return null;
}