Advertisement
Guest User

Wikipedia Article Mode (automatic) userscript

a guest
Jul 24th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2.  
  3. // @name          Wikipedia Article Mode (automatic)
  4. // @namespace      tag:leskets@web.de,2007-10-23:UserScripts
  5. // @description   Toggle between article-mode and normal mode by moving the mouse to the left border or into the article, respectively. Force normal mode by the keyboard shortcut Ctrl+Alt+F.
  6.  
  7. // @include       http://*.wikipedia.org/*
  8. // @include       https://*.wikipedia.org/*
  9. // @include       https://secure.wikimedia.org/wikipedia/*/wiki/*
  10. // @include       http://www.scholarpedia.org/*
  11. // @include       http://www.wikiweise.de/*
  12. // @include       http://www.conservapedia.com/*
  13. // @include       http://www.wikinfo.org/*
  14. // @include       http://athpedia.de/*
  15. // @include       http://www.kathpedia.com/*
  16. // @include       http://en.citizendium.org/*
  17. // @include       http://www.esowatch.com/*
  18. // @include       http://plato.stanford.edu/*
  19. // @include       http://*.wikisource.org/*
  20. // @include       http://*.metapedia.org/*
  21. // @include       http://*.anarchopedia.org/*
  22. // @include       http://creationwiki.org/*
  23. // @include       http://evolutionwiki.org/*
  24. // @include       http://www.sourcewatch.org/*
  25. // @include       http://www.powerbase.info/*
  26. // @include       http://www.lobbypedia.de/*
  27. // @include       http://rationalwiki.org/*
  28. // @include       file:///C:/Users/mtd11/*
  29. //
  30. // @grant GM_getValue
  31. // @grant GM_setValue
  32. // @grant GM_addStyle
  33. // @version       1.4
  34. // ==/UserScript==
  35. // improvements in 1.4:
  36. //  - added @grant
  37. //  - support overlay for mediawikis
  38. // improvements over version 1.2:
  39. //  - added: https://*.wikipedia.org/*
  40. // improvements over version 1.1:
  41. //  - support Chrome (Thanks to Ashok Koyi for sending me a patch)
  42. //  - The search box is only enabled when clicking the top area.
  43. //    This avoids some issues, because disabling the search box does
  44. //    not recover the previous state accurately.
  45.  
  46. /* Modifications to this script is permitted provided this comment is retained in its entirety.
  47.  
  48.  * Copyright: Gautham Pai
  49.  
  50.  * Author: Gautham Pai
  51.  
  52.  * http://buzypi.in/
  53.  
  54.  */
  55. /*
  56.   Original name of the script: Wikipedia Article Mode
  57.   modified by Thomas Leske 2010
  58. */
  59.  
  60. // add the missing GM_*-functions for the browser Chrome:
  61. if (!this.GM_getValue || (this.GM_getValue.toString && this.GM_getValue.toString().indexOf("not supported")>-1)) {
  62.     this.GM_getValue=function (key,def) {
  63.         return localStorage[key] || def;
  64.     };
  65.     this.GM_setValue=function (key,value) {
  66.         return localStorage[key]=value;
  67.     };
  68.     this.GM_deleteValue=function (key) {
  69.         return delete localStorage[key];
  70.     };
  71. }
  72.  
  73. // var enableSearchBoxSwitching = true;
  74.  
  75. var fullscreenShown = true;
  76. var stickyFullscreen = false;
  77.  
  78. var host = window.location.host;
  79. var contentID = (host == 'www.wikiweise.de')
  80.     ? "div-maincontent"
  81.     : ((host == 'www.britannica.com')
  82.        ? "bps-content-panel-body"
  83.        : "content");
  84.  
  85. if (host == 'www.wikiweise.de') {
  86.     inner = document.getElementById("div-maincontent-inner-default");
  87.     inner.setAttribute("style", "max-width: none;")
  88. }
  89.  
  90. var content = document.getElementById(contentID);
  91.  
  92. var usecss = !(host == 'www.wikiweise.de' || host == 'en.citizendium.org' || host == 'www.britannica.com' || host == 'plato.stanford.edu' || host == 'www.conservapedia.com' || host == 'www.wikinfo.org' || host == 'athpedia.de' || host == "www.kathpedia.com" || host == "www.encyclopaediagermanica.com" || host.substring(2,host.length) == '.metapedia.org' || host.substring(3,host.length) == '.anarchopedia.org' || host == 'creationwiki.org' || host == 'www.powerbase.info' || host == 'www.lobbypedia.de')
  93.  
  94. function setDisplayStyle(element_ID, displayStyle) {
  95.   var element = document.getElementById(element_ID);
  96.   if (element) {
  97.       if (!element.style || !element.style.display) {
  98.       var style = document.createAttribute("style");
  99.       style.nodeValue = "display: none;";
  100.       element.setAttributeNode(style);
  101.       }
  102.       element.style.display = displayStyle;
  103.   }
  104. }
  105.  
  106. function getBorderSize() {
  107.     return GM_getValue("borderSize", '0');
  108. }
  109.  
  110. var savedAttributeNode;
  111.  
  112. function adjustBorders() {
  113.     var style = document.createAttribute("style");
  114.     var borderSize = getBorderSize();
  115.     style.nodeValue = "margin-left:" + borderSize + "em; margin-right:" + borderSize + "em";
  116.     content.setAttributeNode(style);
  117. }
  118.  
  119. function switchTo(fullScreen){
  120.     var displayStyle = (fullScreen)
  121.         ? ''
  122.         : 'none';
  123.  
  124.     if (content) {
  125.         if (fullScreen && !usecss) {
  126.         if (savedAttributeNode) {
  127.             content.setAttributeNode(savedAttributeNode);
  128.         } else {
  129.             content.removeAttribute("style");
  130.         }
  131.         } else {
  132.         savedAttributeNode = content.getAttributeNode("style");
  133.         adjustBorders();
  134.         }
  135.     }
  136.  
  137.     if (!usecss) {
  138.       var IDs = [
  139.            // Wikipedia (should use css):
  140.            "mw-panel", "mw-head", "footer",
  141.            "mw-head-base", "mw-page-base",
  142.            // 'www.scholarpedia.org':
  143.            "column-one",
  144.            // 'www.wikiweise.de':
  145.            "div-header-image", "div-footer", "div-sidemenu-frame",
  146.            "div-logininfo", "div-toolbar",
  147.            // 'en.citizendium.org'
  148.           "topcorners", "p-cactions", "p-personal", "p-search", "siteNotice",
  149.            // 'www.newworldencyclopedia.org'
  150.            "p-logo",
  151.            // 'www.britannica.com'
  152.            "bps-header-search", "bps-header", "bps-footer2",
  153.            "bps-sidebar",  "bps-sidebar-content-container", "bps-footer-bottom2",
  154.            "bps-browse-dialog",
  155.            // 'plato.stanford.edu'
  156.            "navmenu", "pagetopleft", "pagetopright",
  157.            ];
  158.  
  159.       for (var id_num = 0; id_num < IDs.length; id_num++) {
  160.           setDisplayStyle(IDs[id_num], displayStyle);
  161.       }
  162.         }
  163.     fullscreenShown = fullScreen;
  164. }
  165.  
  166. function switchToFullScreen() {
  167.    switchTo(true);
  168. }
  169.  
  170. function switchToArticleMode() {
  171.    switchTo(false);
  172. }
  173.  
  174. function toggleStickyFullscreen() {
  175.     if (!stickyFullscreen && !fullscreenShown) {
  176.         switchToFullScreen();
  177.     }
  178.     stickyFullscreen = !stickyFullscreen;
  179. }
  180.  
  181.  
  182. function keyPressEvent(event) {
  183.     var kcode = (event.keyCode)
  184.     ? event.keyCode
  185.     : event.which;
  186.     var ctrlKeyPressed = event.ctrlKey;
  187.     var altKeyPressed  = event.altKey;
  188.     var key = String.fromCharCode(kcode);
  189.  
  190.     if (ctrlKeyPressed && altKeyPressed && (key == 'f' || key == 'F')) {
  191.     toggleStickyFullscreen();
  192.     } else if (!ctrlKeyPressed && altKeyPressed && (key == "+" || key == "-")) {
  193.     if (fullscreenShown) {
  194.         stickyFullscreen = false;
  195.         showArticleOnly(true);
  196.     } else if (content) {
  197.         var borderSize = parseInt(getBorderSize());
  198.         if (key == "+") {
  199.         borderSize = borderSize + 1;
  200.         } else if (borderSize != 0) {
  201.         borderSize = borderSize - 1;
  202.         }
  203.         GM_setValue("borderSize", borderSize);
  204.         adjustBorders();
  205.     }
  206.     }
  207. }
  208.  
  209. document.addEventListener("keypress", keyPressEvent, true);
  210.  
  211. function deselect(element) {
  212.           var temptext = element.value;
  213.           element.value = "";
  214.           element.value = temptext;
  215. }
  216.  
  217. function showArticleOnly(enableSearchBoxSwitching) {
  218.     if (fullscreenShown && !stickyFullscreen) {
  219.         switchToArticleMode();
  220.     if (enableSearchBoxSwitching) {    
  221.         var searchbox = document.getElementById('searchInput');
  222.         if (searchbox) {
  223.           deselect(searchbox);
  224.           searchbox.blur();
  225.         }
  226.     }
  227.     }
  228. }
  229.  
  230. function getPageX(event) {
  231.     if (!event) {
  232.     event = window.event;
  233.     }
  234.     return event.pageX ? event.pageX : event.clientX;
  235. }
  236.  
  237. function getPageY(event) {
  238.     if (!event) {
  239.     event = window.event;
  240.     }
  241.     return event.pageY ? event.pageY : event.clientY;
  242. }
  243.  
  244. showArticleOnly(false);
  245.  
  246. function showArticleWaitingToSwitchBack (enableSearchBoxSwitching) {
  247.     var enableSearchBoxSwitching_tmp = enableSearchBoxSwitching;
  248.     if (!fullscreenShown) {
  249.         switchToFullScreen();
  250.  
  251.     if (content) {
  252.             var handleMouseOverArticle = function (event) {
  253.                showArticleOnly(true);
  254.         }
  255.         content.addEventListener("mouseover", handleMouseOverArticle, true);
  256.     }
  257.     if (enableSearchBoxSwitching) {
  258.         var searchbox = document.getElementById('searchInput');
  259.         if (searchbox) {
  260.         searchbox.focus();
  261.         searchbox.select();
  262.         }
  263.     }
  264.     }
  265. }
  266.  
  267. var inner_border =  20;
  268.  
  269. function handleMove (event) {
  270.   if (getPageX(event) < inner_border) {
  271.       showArticleWaitingToSwitchBack (false);
  272.   }
  273. }
  274.  
  275. function handleClick (event) {
  276.   if (getPageY(event) < inner_border) {
  277.       showArticleWaitingToSwitchBack (true);
  278.   }
  279. }
  280.  
  281. document.addEventListener("mousemove", handleMove, true);
  282. document.addEventListener("mouseup", handleClick, true);
  283.  
  284. if (usecss) {
  285. GM_addStyle("\
  286. #footer, #mw-head-base, #mw-page-base {\
  287.    display: none !important;\
  288. }\
  289. \
  290. #mw-head:hover {\
  291.    overflow: visible !important;\
  292.    height: auto !important;\
  293.    opacity: 0.95;\
  294. }\
  295. \
  296. #mw-head {\
  297.    top: 0 !important;\
  298.    left: 0 !important;\
  299.    overflow: hidden !important;\
  300.    position: fixed !important;\
  301.    right: 0 !important;\
  302.    height: 20px !important;\
  303.    z-index: 10000;\
  304.    opacity: 0;\
  305.    background-color: menu !important;\
  306. }\
  307. \
  308. #mw-panel:hover {\
  309.    overflow: auto !important;\
  310.    opacity: 0.95;\
  311.    width:auto !important;\
  312. }\
  313. \
  314. #mw-panel {\
  315.    top: 0 !important;\
  316.    bottom: 0 !important;\
  317.    overflow: hidden !important;\
  318.    position: fixed !important;\
  319.    left: 0 !important;\
  320.    width:20px !important;\
  321.    z-index: 21000;\
  322.    opacity: 0;\
  323.    background-color: menu !important;\
  324. }\
  325. ")
  326. }
  327.  
  328. GM_addStyle("\
  329. #toc:hover {\
  330.    overflow: scroll !important;\
  331.    opacity: 0.95;\
  332.    width: auto !important;\
  333.    left: auto !important;\
  334.    max-height: 100% !important;\
  335. }\
  336. \
  337. #toc {\
  338.    top: 0 !important;\
  339.    overflow: hidden !important;\
  340.    position: fixed !important;\
  341.    width: 20px !important;\
  342.    left: 99% !important;\
  343.    right: 0 !important;\
  344.    z-index: 20000;\
  345.    opacity: 0;\
  346.    background-color: menu !important;\
  347. }\
  348. ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement