Wayback Machine - toolbar toggler

A way to toggle the WaybackMachine's toolbar

  1. // ==UserScript==
  2. // @name Wayback Machine - toolbar toggler
  3. // @name:fr Wayback Machine - (dé)masquer la barre d'outils
  4. // @namespace https://github.com/Procyon-b
  5. // @version 0.8
  6. // @description A way to toggle the WaybackMachine's toolbar
  7. // @description:fr Une méthode pour masquer/afficher la barre d'outils de WaybackMachine (web.archive.org)
  8. // @author Achernar
  9. // @match *://web.archive.org/web/*
  10. // @grant none
  11. // @run-at document-start
  12. // @noframes
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. "use strict";
  17.  
  18. function addStyle(e,s) {
  19. if (!e) return;
  20. var st=document.createElement("style");
  21. st.textContent=s;
  22. e.appendChild(st);
  23. }
  24.  
  25. function fix(ev) {
  26. var TO, st, sts = document.getElementsByTagName('style');
  27. addStyle( (document.head || document.documentElement), '#wm-ipp-base {opacity:0; min-height:1px;position:absolute;} #wm-ipp-base:hover {opacity: 1;} #wm-ipp-base.forcePriority {oposition:relative; z-index:99999;} body > #wm-ipp-print, body> #donato {display: none;}');
  28.  
  29. // remove this style if present
  30. for (st of sts) {
  31. try{
  32. if (st.innerText.search('.wb-autocomplete-suggestions')+1) {
  33. st.innerText='';
  34. break;
  35. }
  36. }catch(e){}
  37. }
  38.  
  39. // remove all added styles
  40. var wbSt, del=[];
  41. for (let el of document.body.childNodes) {
  42. if (el.nodeType==8) {
  43. if (!wbSt && (el.data==' BEGIN WAYBACK TOOLBAR INSERT ')) wbSt=true;
  44. else if (wbSt && (el.data==' END WAYBACK TOOLBAR INSERT ')) {
  45. for (let i=0; i<del.length; i++) del[i].parentNode.removeChild(del[i]);
  46. break;
  47. }
  48. continue;
  49. }
  50. if (!wbSt) continue;
  51. if (el.nodeName=='STYLE') del.push(el);
  52. }
  53.  
  54. // remove archive.org stylesheets
  55. sts=document.querySelectorAll('link[rel=stylesheet]');
  56. for (st of sts) {
  57. if (st.href.startsWith('https://web.archive.org/_static/') && !(st.href.search('/iconochive.css')+1) ) {
  58. st.parentNode.removeChild(st);
  59. }
  60. }
  61.  
  62. // change "close" icon color
  63. if (!sr) sr=e.sr;
  64. if (sr) {
  65. let s='#wm-tb-close .iconochive-remove-circle {color: #26d926 !important;}';
  66. try{ addStyle(sr, s);
  67. }catch(er){
  68. let st=document.createElement('style');
  69. st.textContent=s;
  70. sr.appendChild(st);
  71. }
  72. }
  73.  
  74. this.removeEventListener('dblclick', fix);
  75. document.body.addEventListener('keydown',function(ev){
  76. force=ev.altKey && ev.ctrlKey;
  77. if (force && !forceState) {
  78. e.classList.add('forcePriority');
  79. forceState=force;
  80. if (TO) clearTimeout(TO);
  81. TO=setTimeout(function(){
  82. e.classList.remove('forcePriority');
  83. forceState=false;
  84. TO=0;
  85. },3000);
  86.  
  87. if (sr) {
  88. let e = sr.querySelector('[style*="display: none;"]')
  89. if (e) e.style.display='';
  90. }
  91. }
  92. });
  93. }
  94.  
  95. var e=document.getElementById('wm-ipp-base'), initCnt=100, sr, TO, force=false, forceState=false;
  96.  
  97. var obs = new MutationObserver(function(mutL) {
  98. for (let mut of mutL) {
  99. let e=mut.addedNodes[0];
  100. if (e && (e.id=='wm-ipp-base')) {
  101. if (TO) clearTimeout(TO);
  102. obs.disconnect();
  103. init();
  104. }
  105. }
  106. });
  107. //obs.observe(document.documentElement, {attributes: false, subtree: true, childList: true });
  108.  
  109. function init() {
  110. e=document.getElementById('wm-ipp-base');
  111. if (!e) {
  112. if (--initCnt > 0) TO=setTimeout(init, 50);
  113. else if (initCnt > -5) TO=setTimeout(init, 3000);
  114. return;
  115. }
  116. obs.disconnect();
  117.  
  118. e.addEventListener('dblclick', fix);
  119. let ne=e.nextElementSibling;
  120. if (ne && (ne.id=='donato')) ne.parentNode.removeChild(ne);
  121. }
  122. //init();
  123.  
  124. // catch attachShadow
  125. var s=document.createElement('script');
  126. s.textContent=`(function(){
  127. var options={attributes: false, subtree: true, childList: true };
  128. var obs = new MutationObserver(function(mutL) {
  129. for (let mut of mutL) {
  130. let e=mut.addedNodes[0];
  131. if (e && (e.id=='wm-ipp-base')) {
  132. var oldAS=e.attachShadow;
  133. e.attachShadow=function(m){
  134. e.sr=oldAS.call(e,m);
  135. return e.sr;
  136. }
  137. obs.disconnect();
  138. }
  139. }
  140. });
  141. obs.observe(document.documentElement, options);
  142. })()`;
  143.  
  144. var injected=0;
  145. function inject() {
  146. if (injected++) return;
  147.  
  148. var sc=document.documentElement.insertBefore(s,document.head);
  149. sc.parentNode.removeChild(sc);
  150. }
  151.  
  152. var started=0;
  153. function start() {
  154. if (started++) return;
  155. inject();
  156. init();
  157. obs.observe(document.documentElement, {attributes: false, subtree: true, childList: true });
  158. }
  159.  
  160. tryUntil(inject, 0);
  161. tryUntil(function(){
  162. obs.observe(document.documentElement, {attributes: false, subtree: true, childList: true });
  163. }, 0);
  164.  
  165. function tryUntil(F, TO=150, c=-1, fail) {
  166. if (!c--) {
  167. fail && fail();
  168. return;
  169. }
  170. try{F();}catch(e){setTimeout(function(){tryUntil(F,TO,c,fail);}, TO)}
  171. }
  172.  
  173. if (document.readyState != 'loading') start();
  174. else {
  175. document.addEventListener('DOMContentLoaded', start);
  176. window.addEventListener('load', start);
  177. }
  178.  
  179.  
  180. })();