FUCKADS/广告标记去除

标记去处sb广告和丑不拉几不想看到的网页元素

  1. // ==UserScript==
  2. // @name FUCKADS/广告标记去除
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description 标记去处sb广告和丑不拉几不想看到的网页元素
  6. // @author Gwen
  7. // @license MIT
  8. // @match *://*/*
  9. // @grant unsafeWindow
  10. // @run-at document-body
  11. // @homepageURL https://gf.qytechs.cn/zh-CN/scripts/473294-ads-%E5%B9%BF%E5%91%8A%E6%A0%87%E8%AE%B0%E5%8E%BB%E9%99%A4
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. console.fuck = function(content) {
  18. console.log('%c' + content, 'color:blue')
  19. }
  20. var savedRules = []
  21. var isSelected = false
  22. var isMaskShow = true
  23. var lastSelectedElement = null
  24. loadRules()
  25. const overlay = document.createElement('div');
  26. overlay.style.position = 'fixed';
  27. overlay.style.top = '0';
  28. overlay.style.left = '0';
  29. overlay.style.width = '100%';
  30. overlay.style.height = '100%';
  31. overlay.style.background = 'rgba(0, 0, 0, 0.5)';
  32. overlay.style.zIndex = '999999999999';
  33. document.body.append(overlay);
  34. const notice = document.createElement('div')
  35. notice.innerText = '点击网页元素进行选择'
  36. notice.style.position = 'fixed';
  37. notice.style.bottom = '50px';
  38. notice.style.left = '20px';
  39. notice.style.zIndex = '1000000999999';
  40. notice.style.fontSize = '18px';
  41. notice.style.fontWeight = '550';
  42. notice.style.color = 'rgb(245, 245, 245)'
  43. notice.style.pointerEvents = 'none';
  44. document.body.append(notice)
  45. const tools = document.createElement('div')
  46. tools.style.position = 'fixed';
  47. tools.style.bottom = '0';
  48. tools.style.left = '20px';
  49. tools.style.zIndex = '1000000999999';
  50. const buttonStyle = `background-color: #4CAF50;border: none;color: white;padding: 10px 20px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;margin: 4px 2px;cursor: pointer;border-radius: 4px;`;
  51. const selectParentButton = createButton("选择父元素", e => {
  52. if (!lastSelectedElement) {
  53. alert('请先点击一个网页元素')
  54. return
  55. }
  56. if (!lastSelectedElement.parentElement || lastSelectedElement.parentElement == document.body) {
  57. alert('父元素不存在')
  58. return
  59. }
  60. clearRectangles()
  61. draw(lastSelectedElement.parentElement)
  62. console.log('选择', lastSelectedElement.parentElement)
  63. lastSelectedElement = lastSelectedElement.parentElement
  64. });
  65. const toggleButton = createButton("隐藏/显示", e => {
  66. if (!lastSelectedElement) {
  67. alert('请先点击一个网页元素')
  68. return
  69. }
  70. let sameClassElements = []
  71. if (lastSelectedElement.className) {
  72. sameClassElements = document.getElementsByClassName(lastSelectedElement.className)
  73. }
  74. if (!lastSelectedElement.style.display || lastSelectedElement.style.display != 'none') { //隐藏元素
  75. lastSelectedElement.style.display = 'none'
  76. for (let elem of sameClassElements) {
  77. elem.style.display = 'none'
  78. }
  79. } else { //显示元素
  80. let originalDisplayType = lastSelectedElement.getAttribute('display-type')
  81. lastSelectedElement.style.display = originalDisplayType
  82. for (let elem of sameClassElements) {
  83. elem.style.display = originalDisplayType
  84. }
  85. }
  86. });
  87. const removeButton = createButton("移除", e => {
  88. if (!lastSelectedElement) {
  89. alert('请先点击一个网页元素')
  90. return
  91. }
  92. let record = confirm('下次进入页面是否阻止其显示?\nSelector: ' + getSelectorPath(lastSelectedElement))
  93. if (record) {
  94. savedRules.push(getSelectorPath(lastSelectedElement))
  95. saveRules()
  96. }
  97. if (lastSelectedElement.className) {
  98. let sameClassElements = document.querySelectorAll(getSelectorPath(lastSelectedElement))
  99. for (let sameClassElement of sameClassElements) {
  100. sameClassElement.remove()
  101. }
  102. } else {
  103. lastSelectedElement.remove();
  104. }
  105. clearRectangles();
  106. });
  107. const showRuleBoxButton = createButton("已记录元素", e => {
  108. if (ruleBox.style.display != 'none') {
  109. ruleBox.style.display = 'none'
  110. } else {
  111. ruleBox.style.display = 'block'
  112. }
  113. })
  114. const exitButton = createButton("退出", e => {
  115. overlay.style.display = 'none';notice.style.display = 'none';tools.style.display = 'none';ruleBox.style.display = 'none';
  116. menuButton.style.display = 'block';
  117. clearRectangles()
  118. lastSelectedElement = null
  119. });
  120. tools.append(selectParentButton)
  121. tools.append(toggleButton)
  122. tools.append(removeButton)
  123. tools.append(showRuleBoxButton)
  124. tools.append(exitButton)
  125. document.body.append(tools)
  126. function createButton(text, clickHandler) {
  127. const button = document.createElement("button");
  128. button.textContent = text;
  129. button.addEventListener("click", clickHandler);
  130. button.style = buttonStyle
  131. return button;
  132. }
  133.  
  134. const ruleBox = document.createElement('div')
  135. ruleBox.style = "position:fixed;bottom:60px;right:10px;z-index:1000000999999;max-height:200px;overflow:auto;"
  136. ruleBox.title = '已存储的广告元素标识'
  137. document.body.append(ruleBox)
  138. function createRuleItem(rule) {
  139. const ruleItem = document.createElement("div");
  140. ruleItem.style = 'background-color:rgba(241,241,241,0.4);padding:5px;margin-bottom:5px;display:flex;align-items:center;width:200px;';
  141. const ruleText = document.createElement("span");
  142. ruleText.style = 'flex-grow:1;word-break:break-all;color:aquamarine';
  143. ruleText.textContent = rule;
  144. ruleItem.appendChild(ruleText);
  145. const deleteButton = document.createElement("button");
  146. deleteButton.style = 'width:20px;height:20px;line-height:20px;text-align:center;background-color:#ccc;color:#fff;border:none;cursor:pointer;';
  147. deleteButton.className = "delete-button";
  148. deleteButton.textContent = "×";
  149. deleteButton.addEventListener("click", () => {
  150. if (!confirm('是否删除该条规则?')) {
  151. return
  152. }
  153. const index = savedRules.indexOf(rule);
  154. if (index !== -1) {
  155. savedRules.splice(index, 1);
  156. ruleItem.remove();
  157. saveRules()
  158. }
  159. });
  160. ruleItem.appendChild(deleteButton);
  161. return ruleItem;
  162. }
  163.  
  164. function renderRuleBox() {
  165. ruleBox.innerHTML = "";
  166. savedRules.forEach(rule => {
  167. const ruleItem = createRuleItem(rule);
  168. ruleBox.appendChild(ruleItem);
  169. });
  170. }
  171. renderRuleBox();
  172.  
  173. const menuButton = document.createElement('div')
  174. menuButton.style = 'position:fixed;z-index:100000000000;right:0;top:150px;width:40px;height:40px;line-height:40px;text-align:center;border-radius:50%;background:red;color:white;font-weight:550;cursor:pointer'
  175. menuButton.textContent = '去广告'
  176. menuButton.addEventListener('click', e => {
  177. overlay.style.display = 'block';notice.style.display = 'block';tools.style.display = 'block';ruleBox.style.display = 'block';
  178. menuButton.style.display = 'none'
  179. })
  180. document.body.append(menuButton)
  181. overlay.style.display = 'none';notice.style.display = 'none';tools.style.display = 'none';ruleBox.style.display = 'none';
  182. menuButton.style.display = 'block'
  183.  
  184. const highlightedRectangles = [];
  185. function loadRules() {
  186. let storage = localStorage.getItem('fuckads')
  187. if (storage) {
  188. savedRules = JSON.parse(storage)
  189. }
  190. }
  191. function saveRules() {
  192. localStorage.setItem('fuckads', JSON.stringify(savedRules))
  193. }
  194.  
  195. function getSelectorPath(element) {
  196. if (!(element instanceof Element)) return;
  197. const originalElement = element
  198. const path = [];
  199. let className = element.className
  200. while (element.parentNode) {
  201. let selector = element.tagName.toLowerCase();
  202. if (element.id && isNaN(element.id)) {
  203. selector += `#${element.id}`;
  204. path.unshift(selector);
  205. break;
  206. } else if (element.className && element.className.trim().length != 0) {
  207. let elementClassName = element.className.trim()
  208. if (elementClassName.indexOf('[') != -1 && className) {
  209. const siblings = Array.from(element.parentNode.children);
  210. const index = siblings.indexOf(element) + 1;
  211. selector += `:nth-child(${index})`;
  212. path.unshift(selector);
  213. } else {
  214. if (element == originalElement) { //如果是目标元素,有class就赶紧记录他的class
  215. selector += '.' + elementClassName.replace(/\s+/g, '.')
  216. path.unshift(selector);
  217. } else if (elementClassName.indexOf(' ') == -1) { //不是目标元素,class太复杂就不要了
  218. selector += `.${elementClassName}`;
  219. path.unshift(selector);
  220. }
  221. }
  222. } else if (!className) {
  223. const siblings = Array.from(element.parentNode.children);
  224. const index = siblings.indexOf(element) + 1;
  225. selector += `:nth-child(${index})`;
  226. path.unshift(selector);
  227. }
  228. element = element.parentNode;
  229. }
  230. return path.join(' ');
  231. }
  232.  
  233. function getElementLeft(element){
  234.   var actualLeft = element.offsetLeft;
  235.   var current = element.offsetParent;
  236.  
  237.   while (current !== null){
  238.     actualLeft += current.offsetLeft;
  239.     current = current.offsetParent;
  240.   }
  241.  
  242. return actualLeft;
  243. }
  244.  
  245.  function getElementTop(element){
  246.   var actualTop = element.offsetTop;
  247.   var current = element.offsetParent;
  248.  
  249.   while (current !== null){
  250.    actualTop += current.offsetTop;
  251.    current = current.offsetParent;
  252.   }
  253.   return actualTop;
  254.  }
  255.  
  256. function draw(element) {
  257. drawRectangle(element)
  258. if (element.className) {
  259. let sameClassElements = document.querySelectorAll(getSelectorPath(element))
  260. for (let sameClassElement of sameClassElements) {
  261. if (element == sameClassElement)
  262. continue
  263. drawRectangle(sameClassElement, 'yellow')
  264. }
  265. }
  266. }
  267.  
  268. function drawRectangle(element, color = 'skyblue') {
  269. element.setAttribute('display-type', element.style.display)
  270. const rect = element.getBoundingClientRect();
  271. const rectangle = document.createElement('div');
  272. let elementPosition = getComputedStyle(element).position
  273. if (elementPosition == 'fixed') {
  274. rectangle.style.position = 'fixed'
  275. } else {
  276. rectangle.style.position = 'absolute'
  277. }
  278. rectangle.style.zIndex = '999999'
  279. rectangle.style.boxShadow = color + ' 0 0 5px 5px'
  280. rectangle.style.boxSizing = 'border-box';
  281. rectangle.style.top = getElementTop(element) + 'px';
  282. rectangle.style.left = getElementLeft(element) + 'px';
  283. rectangle.style.width = element.offsetWidth + 'px';
  284. rectangle.style.height = element.offsetHeight + 'px';
  285. rectangle.style.background = 'rgba(0,255,0,0.3)'
  286. rectangle.style.pointerEvents = 'none'
  287. document.body.append(rectangle);
  288. highlightedRectangles.push(rectangle);
  289. }
  290.  
  291. function clearRectangles() {
  292. for (const rectangle of highlightedRectangles) {
  293. rectangle.remove();
  294. }
  295. highlightedRectangles.length = 0;
  296. }
  297.  
  298. overlay.addEventListener('click', event => {
  299. event = event || window.event;
  300. event.preventDefault ? event.preventDefault() : (event.retrunValue = false) ;
  301. event.stopPropragation ? event.stopPropragation() : (event.cancelBubble = true)
  302. const x = event.clientX;
  303. const y = event.clientY;
  304. overlay.style.pointerEvents = 'none'
  305. const element = document.elementFromPoint(x, y);
  306. overlay.style.pointerEvents = 'all'
  307. if (element && element !== overlay) {
  308. console.fuck(getSelectorPath(element))
  309. if (element != lastSelectedElement) {
  310. clearRectangles()
  311. draw(element)
  312. lastSelectedElement = element
  313. }
  314. }
  315. })
  316.  
  317. const observer = new MutationObserver((mutationsList, observer) => {
  318. for (let mutation of mutationsList) {
  319. if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
  320. mutation.addedNodes.forEach(node => {
  321. if (node.nodeType === Node.ELEMENT_NODE) {
  322. const isAdElement = savedRules.some(selector => node.matches(selector));
  323. if (isAdElement) {
  324. console.log('发现广告元素:', node);
  325. node.remove()
  326. }
  327. }
  328. });
  329. }
  330. }
  331. });
  332. const observerConfig = { childList: true, subtree: true };
  333. observer.observe(document.documentElement, observerConfig);
  334.  
  335. // function fuckEmAll(number) {
  336. // if (number <= 0)
  337. // return
  338. // for (let rule of savedRules) {
  339. // try {
  340. // let elems = document.querySelectorAll(rule)
  341. // for (let elem of elems) {
  342. // elem.remove()
  343. // }
  344. // } catch(err) {
  345. // console.error(err)
  346. // }
  347. // }
  348. // setTimeout(fuckEmAll(number - 1), 100)
  349. // }
  350. // setInterval
  351. window.onload = function(e) {
  352. for (let rule of savedRules) {
  353. try {
  354. let elems = document.querySelectorAll(rule)
  355. for (let elem of elems) {
  356. elem.remove()
  357. }
  358. } catch(err) {
  359. console.error(err)
  360. }
  361. }
  362. }
  363. // fuckEmAll(100)
  364. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址