Blockhead

Blocks headers and other sticky elements from wasting precious vertical screen estate by pinning them down.

目前為 2018-01-06 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Blockhead
  3. // @namespace blockhead
  4. // @description Blocks headers and other sticky elements from wasting precious vertical screen estate by pinning them down.
  5. // @match *://*/*
  6. // @version 16
  7. // @grant GM.getValue
  8. // @grant GM.setValue
  9. // @grant GM_getValue
  10. // @grant GM_setValue
  11. // @grant GM_getResourceText
  12. // @grant GM.getResourceText
  13. // @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
  14. // @xhr-include *
  15. // @author elypter
  16. // @require https://greasyfork.org/scripts/36900-super-gm-setvalue-and-gm-getvalue-lib/code/Super_GM_setValue_and_GM_getValue%20lib.user.js
  17. // @resource black_keywords https://raw.githubusercontent.com/elypter/rule_keyword_generator/master/generic_rule_keywords.txt
  18. // ==/UserScript==
  19.  
  20.  
  21.  
  22. /*
  23. Do you also get annoyed by all those pointless sticky headers with pointless logos, hamburger menus and no particular functionality that can wait for you to scroll up? are you getting flashbacks from your grandmas internet explorer with 10 toolbars installed? do you find it absurd that screens are getting bigger and bigger but there is less and less content to see?
  24. Then get ready to claim back some of that space with this userscript that blocks headers and other sticky elements from wasting precious vertical screen estate by pinning them down to the background of the website. everything is staying acessible but it wont creep up on you like a creeping shower curtain anymore when scrolling.
  25. It checks the computed style of each element. if the position attribute is "fixed" then it checks if its tagname, id or class names contain any keywords provided by the list below. https://raw.githubusercontent.com/elypter/BlockHead/master/black_keywords.js
  26. this list is being created with this tool https://github.com/elypter/rule_keyword_generator by parsing the rules of https://raw.githubusercontent.com/yourduskquibbles/webannoyances/master/ultralist.txt for class names and ids.
  27. There is also a whitelist for certain keywords and tag names to reduce flase positives and processing time.
  28.  
  29. on an 10 year old laptop it can take up 100-200ms or more in extreme cases so the performance aspect is not completely neglegtible but also not a deal breaker.
  30. there is also the option to save automatically generated rules to disk by switching on the save_generated_rules variable in the memory tab of tampermonkey and later putting "blockhead.rules" into the url of any website. the rules will be displayed on the site on a simple div layer so you can copy them where you like. for example into an adbloker or you could help out yourduskquibbles with his webannoyances ultralist https://github.com/yourduskquibbles/webannoyances/issues
  31. https://github.com/elypter/BlockHead/ License: GPL3
  32. */
  33.  
  34. //this style will be added to all elements that this script detects as sticky annoyances.
  35. var stylefix = 'position:relative !important';
  36.  
  37. var count_element_walker=0; //debug:counts how often a page check is triggered
  38. var count_style_walking=0; //debug:counts how often checking stylesheets is triggered
  39.  
  40. //contained in black_keywords.txt
  41. var black_keywords=GM_getResourceText("black_keywords").toString().split("\n"); //list generated with rule_keyword_generator from webannoyances ultralist
  42.  
  43. //id and classes that contain any of these keywords will not be modified
  44. var white_names = ["side","guide","article","html5","story","main","left","right","content","account__section","container--wide","container__main","panel",
  45. "body","gutter","embed","watch","background","middleContainer","drag-and-drop"];
  46. //var white_names = ["example-whitelist-entry"];
  47.  
  48. //tags that will not be cheked
  49. var ignore_tags = ["a","A","script","SCRIPT","body","BODY","li","LI","ul","UL","br","BR","h5","H5","b","B","strong","STRONG","svg","SVG","path","PATHH","h2","H2",
  50. "code","CODE","tr","TR","td","TD","h3","H3","h1","H1","h4","H4"];//,"noscript","NOSCRIPT"
  51.  
  52. //debug switch. if on it prints out information in the java script console.
  53. var debug=GM_SuperValue.get ("debug")==true?GM_SuperValue.get ("debug"):0; //1=yes 2=no 3=intense change value in memory tab
  54. GM_SuperValue.set ("debug",debug);
  55.  
  56. //search for floating elements that get added after page load.
  57. var mutation_check=GM_SuperValue.get ("mutation_check")==true?GM_SuperValue.get ("mutation_check"):1; //1=yes 2=no change value in memory tab
  58. GM_SuperValue.set ("mutation_check",mutation_check);
  59.  
  60. //all elements of a site will be checked individually for their computed style and changed if there is a match
  61. var walk_elements=GM_SuperValue.get ("walk_elements")==true?GM_SuperValue.get ("walk_elements"):1; //1=yes 2=no change value in memory tab
  62. GM_SuperValue.set ("walk_elements",walk_elements);
  63.  
  64. //all stylesheets will be checked for classes and changed if there is a match
  65. var walk_styles=GM_SuperValue.get ("walk_styles")==true?GM_SuperValue.get ("walk_styles"):0; //1=yes 2=no change value in memory tab
  66. GM_SuperValue.set ("walk_styles",walk_styles);
  67.  
  68. //this will save the statistics of how often a keyword is being matched in a local variable that can be viewed in the memory tab
  69. var save_keyword_statistics=GM_SuperValue.get ("save_keyword_statistics")==true?GM_SuperValue.get ("save_keyword_statistics"):0; //1=yes 2=no change value in memory tab
  70. GM_SuperValue.set ("save_keyword_statistics",save_keyword_statistics);
  71.  
  72. //this will save the rules generated based on the blocking in a local variable that can be viewed in the memory tab
  73. var save_generated_rules=GM_SuperValue.get ("save_generated_rules")==true?GM_SuperValue.get ("save_generated_rules"):0; //1=yes 2=no change value in memory tab
  74. GM_SuperValue.set ("save_generated_rules",save_generated_rules);
  75.  
  76.  
  77.  
  78. function counted_element_walker(elm,orig){
  79. count_element_walker++;
  80.  
  81. //this disables all javascript that is being triggered when scrolling
  82. window.addEventListener("scroll", function (event) {
  83. event.stopPropagation();
  84. }, true);
  85. console.log("check number "+count_element_walker+" from: "+orig);
  86. element_walker_all(elm);
  87. if (debug==3) console.log(GM_SuperValue.get ("white_names_counter"));
  88. if (debug==3) console.log(GM_SuperValue.get ("black_keywords_counter"));
  89. if (debug==3) console.log(GM_SuperValue.get ("generated_rules"));
  90. }
  91.  
  92. function keyword_walker(keyword_list){
  93. var white_names_counter=GM_SuperValue.get ("white_names_counter")||{};
  94. var black_keywords_counter=GM_SuperValue.get ("black_keywords_counter")||{};
  95.  
  96. //todo: switch order of for loops to detect whitelists earlier
  97. var state=-1;
  98. if (debug==3) console.log("list: ("+keyword_list.length+") "+keyword_list);
  99. for (var i=0; i < keyword_list.length; i++){
  100. var subword_list=keyword_list[i].toString().split('-');
  101. if (debug==3) console.log("sublen of: "+subword_list+" from "+keyword_list[i]+" = "+subword_list.length);
  102. for (var j=0; j < subword_list.length; j++){
  103. var subsubword_list=subword_list[j].split('_');
  104. if (debug==3) console.log("subsublen of: "+subsubword_list+" from "+subword_list[j]+" = "+subsubword_list.length);
  105. for (var k=0; k < subsubword_list.length; k++){
  106. for (var l=0; l < white_names.length; l++){
  107. if (debug==3) console.log("test white: l:"+white_names[l]+" in s:"+subsubword_list[k]+" of "+keyword_list);
  108. if (subsubword_list[k].indexOf(white_names[l]) != -1){
  109. if (debug==3) console.log("whitelisted: l:"+white_names[l]+" in s:"+subsubword_list[k]+" of "+keyword_list);
  110. if(white_names_counter[white_names[l]]) white_names_counter[white_names[l]]++;
  111. else white_names_counter[white_names[l]]=1;
  112. GM_SuperValue.set ("white_names_counter", white_names_counter);
  113. return 0;
  114. }
  115. }
  116. for (var l=0; l < black_keywords.length; l++){
  117. if (subsubword_list[k].indexOf(black_keywords[l]) != -1){
  118. if (debug==3||debug==1) console.log("matched: l:"+black_keywords[l]+" in s:"+subsubword_list[k]+" of "+keyword_list);
  119. if(black_keywords_counter[black_keywords[l]]) black_keywords_counter[black_keywords[l]]++;
  120. else black_keywords_counter[black_keywords[l]]=1;
  121. state = 1;
  122. }
  123. }
  124. }
  125. }
  126. }
  127.  
  128. GM_SuperValue.set ("black_keywords_counter", black_keywords_counter);
  129. return state;
  130. }
  131.  
  132. function element_walker_all(startElem) {
  133. //walk over element list as opposed to an element tree
  134. if (!(startElem instanceof Element)) return;
  135.  
  136. var generated_rules=GM_SuperValue.get ("generated_rules")||[]; //contains all adblock/ublock rules that the script creates based on what it modifies
  137.  
  138. var elms = startElem.getElementsByTagName("*");
  139. for (var x = elms.length; x--;) {
  140. elm=elms[x];
  141. if (debug==3) console.log("checking: "+elm.tagName.toString());
  142. if(elm instanceof Element && ignore_tags.indexOf(elm.tagName.toString()) == -1 && getComputedStyle(elm)) {
  143. if (debug==3) console.log("testing: "+elm.tagName.toString()+" with position= "+getComputedStyle(elm).getPropertyValue("position").toLowerCase());
  144. if (/*(getComputedStyle(elm).getPropertyValue("position") == "absolute") ||*/
  145. (getComputedStyle(elm).getPropertyValue("position").toLowerCase() == "fixed")/* || */
  146. /*(getComputedStyle(elm).getPropertyValue("position") == "relative")*//* ||*/
  147. /*(getComputedStyle(elm).getPropertyValue("top") != "")*/) {
  148.  
  149. var keyword_list =[];
  150. keyword_list=elm.className.toString().split(' ');
  151. if (typeof(elm.id)=="string"&&elm.id!="") keyword_list.push([elm.id]);
  152. if (typeof(elm.tagName)=="string"&&elm.tagName!="") keyword_list.push([elm.tagName]);
  153.  
  154. var has_matched=-1;
  155. if (keyword_list!=[]&&keyword_list!=[""]){
  156. //compare against black and whitelist
  157. has_matched=keyword_walker(keyword_list);
  158. }
  159. var rule;
  160. var class_list=elm.className.toString().split(' '); //check for rule writing
  161. if (has_matched==1){
  162. if (debug==3||debug==1) console.log("pinning sticky: "+elm.id+","+elm.className+","+elm.tagName);
  163.  
  164. if (elm.id){
  165. rule=window.location.hostname+"###"+elm.id+":style(position: "+"fixed"+" !important;)";
  166. if(generated_rules.indexOf(rule)==-1){
  167. if(!(rule in generated_rules)) generated_rules.push(rule);
  168. //console.log(rule);
  169. }
  170. }
  171. if(elm.className){
  172. for (var i=0; i < class_list.length; i++){
  173. rule=window.location.hostname+"##."+class_list[i]+":style(position: "+"fixed" +" !important;)";
  174. if(generated_rules.indexOf(rule)==-1){
  175. if(!(rule in generated_rules)) generated_rules.push(rule);
  176. //console.log(rule);
  177. }
  178. }
  179. }
  180. elm.setAttribute('style', stylefix);
  181. elm.style.removeProperty('top');
  182. //return;
  183. }else if(has_matched==0){
  184. if (elm.id){
  185. rule=window.location.hostname+"#@#"+elm.id;
  186. if(generated_rules.indexOf(rule)==-1){
  187. if(!(rule in generated_rules)) generated_rules.push(rule);
  188. //console.log(rule);
  189. }
  190. }
  191. if(elm.className){
  192. for (var j=0; j < class_list.length; j++){
  193. rule=window.location.hostname+"#@."+class_list[j];
  194. if(generated_rules.indexOf(rule)==-1){
  195. if(!(rule in generated_rules)) generated_rules.push(rule);
  196. //console.log(rule);
  197. }
  198. }
  199. }
  200. if (debug==3||debug==1) console.log("whitelisted sticky: "+elm.id+","+elm.className+","+elm.tagName);
  201. }else{
  202. if (debug==3||debug==1) console.log("ignoring sticky: "+elm.id+","+elm.className+","+elm.tagName);
  203. }
  204. }
  205. }
  206.  
  207.  
  208.  
  209. }
  210. GM_SuperValue.set ("generated_rules", generated_rules);
  211. }
  212.  
  213. function element_walker(elm) {
  214. //walk over element list as opposed to an element tree
  215. var node;
  216. //console.log("checking: "+elm.tagName.toString());
  217. if(elm instanceof Element && ignore_tags.indexOf(elm.tagName.toString()) == -1 && getComputedStyle(elm)) {
  218. //console.log("testing: "+elm.tagName.toString()+" with position= "+getComputedStyle(elm).getPropertyValue("position").toLowerCase());
  219. if (/*(getComputedStyle(elm).getPropertyValue("position") == "absolute") ||*/
  220. (getComputedStyle(elm).getPropertyValue("position").toLowerCase() == "fixed")/* || */
  221. /*(getComputedStyle(elm).getPropertyValue("position") == "relative")*//* ||*/
  222. /*(getComputedStyle(elm).getPropertyValue("top") != "")*/) {
  223.  
  224. var keyword_list =[];
  225. keyword_list=elm.className.toString().split(' ');
  226. if (typeof(elm.id)=="string"&&elm.id!="") keyword_list.push([elm.id]);
  227.  
  228. var has_matched=-1;
  229. if (keyword_list!=[]&&keyword_list!=[""]){
  230. has_matched=keyword_walker(keyword_list);
  231. }
  232. var rule;
  233. var class_list=elm.className.toString().split(' '); //check for rule writing
  234. if (has_matched==1){
  235. if (debug==3) console.log("pinning sticky: "+elm.id+","+elm.className+","+elm.tagName);
  236.  
  237. if (elm.id){
  238. rule=window.location.hostname+"###"+elm.id+":style(position: "+"fixed"+" !important;)";
  239. if(generated_rules.indexOf(rule)==-1){
  240. generated_rules.push(rule);
  241. if (debug==3||debug==1) console.log(rule);
  242. }
  243. }
  244. if(elm.className){
  245. for (var i=0; i < class_list.length; i++){
  246. rule=window.location.hostname+"##."+class_list[i]+":style(position: "+"fixed" +" !important;)";
  247. if(generated_rules.indexOf(rule)==-1){
  248. generated_rules.push(rule);
  249. if (debug==3||debug==1) console.log(rule);
  250. }
  251. }
  252. }
  253. elm.setAttribute('style', stylefix);
  254. elm.style.removeProperty('top');
  255. //return;
  256. }else if(has_matched==0){
  257. if(elm.className){
  258. for (var j=0; j < class_list.length; j++){
  259. rule="@@"+window.location.hostname+"##."+class_list[j];
  260. if(generated_rules.indexOf(rule)==-1){
  261. generated_rules.push(rule);
  262. if (debug==3||debug==1) console.log(rule);
  263. }
  264. }
  265. }
  266. //return;
  267. }else{
  268. if (debug==3) console.log("ignoring sticky: "+elm.id+","+elm.className+","+elm.tagName);
  269. }
  270. }
  271. }
  272.  
  273. // Handle child elements
  274. for (node = elm.firstChild; node; node = node.nextSibling) {
  275. if (node.nodeType === 1) { // 1 == Element
  276. element_walker(node);
  277. }
  278. }
  279.  
  280.  
  281. }
  282.  
  283. function style_walker() {
  284. //this alternative mode of searching and modifying sticky elements by searching stylesheets directly
  285. //although i thought stylesheet checking would be faster it tends be slower and cannot be performed on external stylesheets
  286.  
  287. var state=0;
  288. count_style_walking++;
  289. if (debug==3) console.log("checking stylesheets for the "+count_style_walking+"th time");
  290. var styleSheets = window.document.styleSheets;
  291.  
  292. for(var i = 0; i < styleSheets.length; i++){
  293. //console.log("checking stylesheet #"+i);//+" named "+styleSheets[i].sheet);
  294.  
  295. //try to get a list of classes for each stylesheet.
  296. //this will throw an error if the stylesheet is hosted on an external server because of cross site protection
  297. //these stylesheets cannot be processed at them moment/or never
  298. var classes;
  299. if (document.styleSheets[i].cssRules){
  300. classes=document.styleSheets[i].cssRules;
  301. }else if (document.styleSheets[i].rules){
  302. classes=document.styleSheets[i].rules;
  303. }//}catch(e){}
  304. if (!classes) continue;
  305.  
  306. for (var j = 0; j < classes.length; j++) {
  307. if (debug==3) console.log("checking class "+classes[x].selectorText);
  308. state=0;
  309. for (var k=0; k < black_keywords.length; k++){
  310. if (classes[j].selectorText) if (classes[j].selectorText.indexOf(black_keywords[k])!=-1){
  311. if (debug==3) console.log("matched: l:"+black_keywords[k]+" in s:"+classes[j].selectorText+" of "+styleSheets[i].sheet);
  312. if (classes[j].position=="absolute" ||classes[j].position=="fixed"){
  313. state = 1;
  314. }
  315. }
  316. }
  317. for (var k=0; k < white_names.length; k++){
  318. if (classes[j].selectorText) if (classes[j].selectorText.indexOf(white_names[k])!=-1){
  319. if (debug==3) console.log("whitelisted: l:"+white_names[k]+" in s:"+classes[j].selectorText+" of "+styleSheets[i].sheet);
  320. state=0;
  321. }
  322. }
  323. if (state==1){
  324. stylesheet.deleteRule("position");
  325. }
  326. }
  327. }
  328. }
  329.  
  330. // show saved rules when opening a location containing "bloackhead-rules"
  331. if(window.location.toString().indexOf("blockhead-rules")!=-1){
  332. var iDiv = document.createElement('div');
  333. iDiv.id = 'blockhead-rules';
  334. iDiv.style.margin = '0 auto';
  335. iDiv.style.position= "absolute";
  336. iDiv.style.backgroundColor ="white";
  337. iDiv.style.zIndex=9001000;
  338. iDiv.style.opacity=1;
  339. document.getElementsByTagName('body')[0].appendChild(iDiv);
  340.  
  341. rules=GM_SuperValue.get ("generated_rules");
  342. for(var i=0;i<rules.length;i++){
  343. iDiv.innerHTML+=rules[i]+'<br/>\n';
  344. }
  345. throw new Error('This is not an error. This is just to abort javascript');
  346. }
  347. // show saved rules when opening a location containing "bloackhead-statistics"
  348. if(window.location.toString().indexOf("blockhead-statistics")!=-1){
  349. var iDiv = document.createElement('div');
  350. iDiv.id = 'blockhead-statistics';
  351. iDiv.style.margin = '0 auto';
  352. iDiv.style.position= "absolute";
  353. iDiv.style.backgroundColor ="white";
  354. iDiv.style.zIndex=9001000;
  355. iDiv.style.opacity=1;
  356. document.getElementsByTagName('body')[0].appendChild(iDiv);
  357.  
  358. iDiv.innerHTML='#white_names_counter:<br/>\ņ';
  359. rules=GM_SuperValue.get ("white_names_counter");
  360. for (var key in rules) {
  361. iDiv.innerHTML+=key+": "+rules[key]+'<br/>\n';
  362. }
  363. iDiv.innerHTML+='<br/>\n############<br/>\n<br/>\n';
  364. iDiv.innerHTML+='#black_keyword_counter:<br/>\ņ';
  365. rules=GM_SuperValue.get ("black_keywords_counter");
  366. for (var key in rules) {
  367. iDiv.innerHTML+=key+": "+rules[key]+'<br/>\n';
  368. }
  369. throw new Error('This is not an error. This is just to abort javascript');
  370. }
  371.  
  372. // Kick it off starting with the `body` element
  373. if(walk_styles==1) style_walker();
  374. if(walk_elements==1) counted_element_walker(document.body,"onstart");
  375.  
  376. // check elements again if the page code changes
  377. if(walk_elements==1) document.onload = counted_element_walker(document.body,"onload");
  378.  
  379. // check elements again if the page code changes
  380. if(walk_elements==1) document.onchange = counted_element_walker(document.body,"onchange");
  381.  
  382. // check elements again if the user scrolls the page(doesnt really do anything)
  383. if(walk_elements==1) document.onscroll = counted_element_walker(document.body,"onscroll");
  384.  
  385.  
  386.  
  387. //check if the dom is modified and checks the changed elements again
  388. if(mutation_check==1 && walk_elements==1){
  389. MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
  390.  
  391. // define a new observer
  392. var obs = new MutationObserver(function(mutations, observer) {
  393. // look through all mutations that just occured
  394. for(var i=0; i<mutations.length; ++i) {
  395. for(var j=0; j<mutations[i].addedNodes.length; ++j) {
  396. //check this mutation
  397. counted_element_walker(mutations[i].addedNodes[j],"onmutation");
  398. }
  399. }
  400. });
  401.  
  402. // have the observer observe for changes in children
  403. obs.observe(document.body, {
  404. attributes : true, //check for attribute changes
  405. childList: true, //use a list of occured changes
  406. subtree: false //also return all subelements of a change
  407. });
  408. }