Regwall Element Blocker

Blocks rendering of elements with class or id containing "regwall"

当前为 2023-06-15 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Regwall Element Blocker
  3. // @namespace regwall-element-blocker
  4. // @version 1.2
  5. // @description Blocks rendering of elements with class or id containing "regwall"
  6. // @match https://www.economist.com/*
  7. // @match *://*.fortune.com/*
  8. // @run-at document-start
  9. // @author TIME
  10. // @license MIT
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. let loadCustomPage = () => {
  18. var xhr = new XMLHttpRequest();
  19.  
  20. xhr.open("GET", window.location.href, true);
  21.  
  22. xhr.onerror = function () {
  23. document.documentElement.innerHTML = "Error getting Page!";
  24. };
  25.  
  26. xhr.send();
  27.  
  28. xhr.onreadystatechange = function() {
  29. if (this.readyState == 4 && this.status == 200) {
  30. document.documentElement.innerHTML = "Removing the Subscription...";
  31. removeSubscription(this.responseText);
  32. }
  33. else if(this.readyState == 0){
  34. document.documentElement.innerHTML = "Initiating the Request...";
  35. }
  36. else if(this.readyState == 1){
  37. document.documentElement.innerHTML = "Establishing the Server...";
  38. }
  39. else if(this.readyState == 2){
  40. document.documentElement.innerHTML = "Request Received...";
  41. }
  42. else if(this.readyState == 3){
  43. document.documentElement.innerHTML = "Processing the Request...";
  44. }
  45. else{
  46. document.documentElement.innerHTML = "Error Finding the Page!";
  47. }
  48. };
  49. };
  50.  
  51. let removeSubscription = (htmlContentStr) => {
  52. let wrapper = document.createElement("DIV");
  53. wrapper.innerHTML = htmlContentStr;
  54. if (matchDomain('economist.com')) {
  55. let paywalls = wrapper.querySelectorAll(".paywall");
  56. let subscriptions = wrapper.querySelectorAll(".subscription-benefits");
  57.  
  58. paywalls.forEach((paywall) => {
  59. paywall.remove();
  60. });
  61. subscriptions.forEach((subscription) => {
  62. subscription.remove();
  63. });
  64. } else if (matchDomain('fortune.com')) {
  65. // Hide elements with class 'tp-container-inner' by adding 'display: none' to their style attribute
  66. let tpContainerInnerElements = wrapper.querySelectorAll('.paywall-selector paywallFade');
  67. for (let i = 0; i < tpContainerInnerElements.length; i++) {
  68. tpContainerInnerElements[i].style.display = 'none';
  69. };
  70. };
  71.  
  72. document.documentElement.innerHTML = "Removing the Ads...";
  73. removeAds(wrapper.innerHTML);
  74. };
  75.  
  76. let removeAds = (htmlContentStr) => {
  77. let wrapper = document.createElement("DIV");
  78. wrapper.innerHTML = htmlContentStr;
  79.  
  80. let adverts = wrapper.querySelectorAll(".advert");
  81. adverts.forEach((advert) => {
  82. advert.remove();
  83. });
  84.  
  85. putNewPage(wrapper);
  86. };
  87.  
  88. let putNewPage = (pageHtml) => document.documentElement.innerHTML = pageHtml.innerHTML;
  89.  
  90. window.stop();
  91. loadCustomPage();
  92.  
  93. function matchDomain(domains) {
  94. const hostname = window.location.hostname;
  95. if (typeof domains === 'string') { domains = [domains]; }
  96. return domains.some(domain => hostname === domain || hostname.endsWith('.' + domain));
  97. }
  98.  
  99. })();
  100.