User:Woganhemlock/searchFix.js

/* Search Fix

Original source - http://community.wikia.com/wiki/User:Daniel_Letalis/global.js - revision as of 5th May 2012

NOTE - ADDING THIS TO A SKIN OTHER THAN YOUR PERSONAL SKIN IS A TOU VIOLATION /*global $, skin, addOnloadHook */ /*jslint devel: true, browser: true, indent: 2, plusplus: true, bitwise: true, white: true */ addOnloadHook( function {  'use strict';  if (skin !== 'oasis') { return; }  var    get_cookie = function  {      var i, c, cl = document.cookie.split(';');      for (i = 0; i < cl.length; i++) {        c = $.trim(cl[i]);        if (c.indexOf('search_fix=') === 0) { return parseInt(c.substring(11), 10) || 0; }      }      return 1;    },    set_cookie = function (val) {      var date = new Date;      date.setTime(date.getTime + (30 * 24 * 60 * 60 * 1000));      date = date.toGMTString;      document.cookie = 'search_fix=' + val + '; expires=' + date + '; domain=.wikia.com; path=/';      return val;    },    settings,    $original_search_button = $('#WikiaSearch button.secondary:first'),    $go_button = $original_search_button.clone,    $options_button = $original_search_button.clone,    $search_text = $('#WikiaSearch input[name="search"]'),    go_button_arrow = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABUAAAAQCAYAAAD52jQlAAAAAXNSR0IArs4c6QAAAFNJREFUOMtjYBgsoLq6+gw+eUZKDGxtbTWhiqHEGMxIjSBAN5iRWmGLbDAjNSMNZjAjMbFJCmhtbTVhokWSo533B01EUT1JUT3xE5NNaVKg0AQAAB9RMIjCqha0AAAAAElFTkSuQmCC', go_button_load = 'data:image/gif;base64,R0lGODlhFQAQAIQAAP///wAAAPDw8IqKiuDg4EZGRnp6egAAAFhYWCQkJKysrL6+vhQUFJycnAQEBDY2NmhoaP///////////////////////////////////////////////////////////yH/C05FVFNDQVBFMi4wAwEAAAAh+QQJCgAfACwCAAAAEAAQAAAFd+AnfgIJIeWofkRCCMdBkKtIHIngyMKsErPBYbADpkSCwhDmQCBethRB6Vj4kFCkQPG4IlWDgrNRIwnO4UKBXDufzQvDMaoSDBgFb886MiQadgNABAokfCwzBA8LCg0Egl8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UrY4ICHH1dSoTFgcHUiZjBhAJB2AHDykpKAwHAwdzf19KkASIPl9cDgcnDkdtNwiMJCshACH5BAkKAB8ALAIAAAAQABAAAAV34Cd+AkkQZTmOAiosiyAoxCq+KPxCNVsSMRgBsiClWrLTSWFoIQZHl6pleBh6suxKMIhlv7bPwkBWfFWrBQTxNLq2RG2yhSUkDs2b63AYDAoJXAcFRx8DeAkJDX0fQCsEfAQMDAIPBz0rCgcxky0JRWE1AmwpKyEAIfkECQoAHwAsAgAAABAAEAAABXngJ34CKZzkqJ4nQZxLqZKv4NqNLKK2/Q4Ek4lFXChsg5ypJjs1II3gEDUSRInE2Ycw6B6zM4JhrDAtEosVkLUtHA7RHaHAGJQEjsODcEg0FBAFVgkQJQ1pAwcDDw8KcFtSInwJAowCCA6RIwqZAgkPNgVpWndjdyohACH5BAkKAB8ALAIAAAAQABAAAAV54Cd+Aimc5KieLEuUKvm2xAKLqDCfC2GaO9eL0LABWTiBYmA06W6kHgvCqEJiH4Jiu3gcvgUsscHUERm+kaCxyxa+zRPk0SgJEgfIvbAdIAQLCAYlCj4DBw0IBQsMCjIqBAcPAooCBg9pKgsJLwUFOhCZKyQDA3YqIQAh+QQJCgAfACwCAAAAEAAQAAAFdeAnfgIpnOSonmxbqiThCrJKEHFbo8JxDDOZYFFb+A41E4H4OhkOipXwBElYIbDPckFEOBgMQ3arkMkUBdxIUGZpEb7kaQBRlASPg0FQQHAbEEMGDSVEHw1QBhAED1E0NgwFAooCDWljaQIQCE5qMHcNhCkjIQAh+QQBCgAfACwCAAAAEAAQAAAFeeAnfgIpnOSoLgxxvqj6LUcCC65KEB9yKK8cSpA4DAiHQ/DkKhGKh4ZCtCyZGo6F6iYYPAqFgYy02xkSaLEMV34tELyRYNEsCQyHlvWkGCzsPgMCEAY7Cg04Uk48LAsDhRA8MVQPEF0GAgqYYwSRlycNcWskCkApIyEAOw==', search_pos, search_text, do_go = function (t, external_search) { var camelcase = function (t, all) { if (typeof all === 'undefined') { all = false; } /*jslint regexp: true */ t = t.replace(/^(.)|\s(.)|-(.)|_(.)|\/(.)/g, function ($1) { return $1.toUpperCase; }); /*jslint regexp: false */ if (all) { return t; } return t.replace(/( (?:a|an|and|as|at|by|for|from|in|of|on|some|that|the|to|with) )/gi, function ($1) { return $1.toLowerCase; }); },       x, case_variations = {}, titles = [], settings = get_cookie; if (typeof external_search === 'undefined') { external_search = false; } t = $.trim(t); if (t !== '') { $go_button.attr('disabled', 'disabled').find('img').attr('src', go_button_load); case_variations[t.substr(0, 1).toUpperCase + t.substr(1)] = 0; case_variations[camelcase(t)] = 0; case_variations[camelcase(t, true)] = 0; case_variations[t.toLowerCase] = 0; case_variations[t.toUpperCase] = 0; for (x in case_variations) { if (case_variations.hasOwnProperty(x)) { titles.push(encodeURIComponent(x)); } } if (settings & 8) { for (x in case_variations) { if (case_variations.hasOwnProperty(x)) { titles.push(encodeURIComponent('Category:' + x)); } } }       $.ajax({          'url': '/api.php?action=query&titles=' + (titles.join('|')) + (settings & 8 ? '&prop=categoryinfo' : '') + '&format=json',         'dataType': 'json',          'type': 'GET',          'async': (external_search || !(settings & 2)),          'timeout': 5000,          'success': function (o) {            //alert(JSON.stringify(o, ' ', 2));            var x, pages, url;            if (o.hasOwnProperty('query') && o.query.hasOwnProperty('pages')) {              pages = o.query.pages;              for (x in pages) { if (pages.hasOwnProperty(x)) {                if (parseInt(x, 10) > 0 && !pages[x].hasOwnProperty('categoryinfo')) {                  url = 'http://' + location.host + '/wiki/' + encodeURI(pages[x].title);                  if (!external_search && settings & 2) { window.open(url); }                  else { window.location.href = url; }                  return;                }              } }              if (settings & 8) {                for (x in pages) { if (pages.hasOwnProperty(x) && pages[x].hasOwnProperty('categoryinfo')) { url = 'http://' + location.host + '/wiki/' + encodeURI(pages[x].title); if (!external_search && settings & 2) { window.open(url); } else { window.location.href = url; } return; } }             }              if (!external_search) { $original_search_button.click; }           }          },          'error': function  { }, 'complete': function { $go_button.removeAttr('disabled').find('img').attr('src', go_button_arrow); }       });      }    };  /* external search */  if (location.search.indexOf('search_fix_go=1') !== -1 && location.search.indexOf('search=') !== -1) {    search_pos = location.search.indexOf('search=') + 7;    search_text = decodeURIComponent(location.search.substr(search_pos).split(/&|;/)).replace(/\+/g, ' ');    do_go(search_text, true);  }  if ($('#WikiaSearch').size < 1) { return; }  /*adding buttons*/  $original_search_button.after($options_button).before($go_button);  $go_button.add($options_button).find('img')    .removeClass('sprite search')    .attr('alt', function (i) { return ['Go', 'Options'][i]; })    .attr('src', function (i) { return [ go_button_arrow, 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAQCAYAAAAmlE46AAAAAXNSR0IArs4c6QAAAB1JREFUKM9jYBgFo2CkgOrq6jPV1dVniOUz0d2FAHu/DXFavA0iAAAAAElFTkSuQmCC' ][i]; })   .add($original_search_button.find('img'))    .height(16).width(function (i) { return [21, 21, 10][i]; })    .parent.css('padding', '0px 6px');  $go_button.css('right', '+=' + ($original_search_button.outerWidth + $options_button.outerWidth));  $original_search_button.css('right', '+=' + $options_button.outerWidth);  $search_text.css('padding-right', '+=' + (($original_search_button.outerWidth + $options_button.outerWidth)))    .width($search_text.width - ($original_search_button.outerWidth + $options_button.outerWidth));  /*removing auto complete*/  $(document).ready(function  { if (get_cookie & 4) { $.loadJQueryAutocomplete = function  { }; } });  /*events*/  $go_button.click(function  { do_go($search_text.val); return false; }); $original_search_button.click(function  { $('#WikiaSearch').attr('target', get_cookie & 2 ? '_blank' : '_self'); $('#WikiaSearch').submit; return false; }); $options_button.click(function  { var check_settings = function (name, num) { if (settings & num) { $('#search_fix_opt_' + name).attr('checked', 'checked'); } else { $('#search_fix_opt_' + name).removeAttr('checked'); } };   if ($('#search_fix_options').size < 1) { $('#WikiaSearch').after(       $(' ', {'id': 'search_fix_options', 'class': 'asasamodule'})          .css({ 'background': '#FFFFFF', 'border': '1px solid #CCCCCC', 'box-shadow': '0px 2px 5px 0px #E0E0E0', 'display': 'none', 'line-height': '1.5em', 'margin-bottom': '10px', 'max-width': '282px', 'padding': '8px', 'position': ($('#WikiaRail #WikiaSearch').size ? 'relative' : 'absolute'), 'right': ($('#WikiaRail #WikiaSearch').size ? 0 : 10) + 'px', 'top': ($('#WikiHeader #WikiaSearch').size ? 83 : ($('#WikiaArticle #WikiaSearch').size ? 38 : 0)) + 'px' })         .append( 'What to use when you press Enter:', $(' '),           $(' ', {'type': 'radio', 'id': 'search_fix_opt_search', 'value': '0', 'name': 'search_fix_default_button', 'checked': 'checked'}), 'Search', $(' '),           $(' ', {'type': 'radio', 'id': 'search_fix_opt_go',  'value': '1', 'name': 'search_fix_default_button'}), 'Go', $(' '),           $(' '),            $(' ', {'type': 'checkbox', 'value': '1', 'id': 'search_fix_opt_new_window', 'name': 'search_fix_new_window'}), 'Open all searches in a new window', $(' '),           $(' ', {'type': 'checkbox', 'value': '1', 'id': 'search_fix_opt_acomplete', 'name': 'search_fix_acomplete'}), 'Disable wikia autocomplete (for future pages)', $(' '),           $(' ', {'type': 'checkbox', 'value': '1', 'id': 'search_fix_opt_cat', 'name': 'search_fix_cat'}), 'Include categories in Go', $(' ', {'id': 'search_fix_options_save', 'value': '1', 'name': 'search_fix_new_window'}) .text('Save') .css({'top': '30%', 'right': '18px', 'position': 'absolute'}) .click(function {                settings = set_cookie( parseInt($('#search_fix_options input[name="search_fix_default_button"]:checked').val, 10) + ($('#search_fix_opt_new_window').is(':checked') ? 2 : 0) + ($('#search_fix_opt_acomplete').is(':checked') ? 4 : 0) + ($('#search_fix_opt_cat').is(':checked') ? 8 : 0) );               $('#search_fix_options').slideUp('fast');              }) )     );    }    settings = get_cookie; $('#' + (settings & 1 ? 'search_fix_opt_go' : 'search_fix_opt_search')).click; check_settings('new_window', 2); check_settings('acomplete', 4); check_settings('cat', 8); $('#search_fix_options').slideToggle('fast'); return false; }); $('#WikiaSearch input:submit').click(function  { settings = get_cookie; if (settings & 1) { $go_button.click; } else { $original_search_button.click; } return false; }); });