// ==UserScript== // @name 添加自定义css和js(广告屏蔽等) // @description 可自定义css选择器屏蔽页面广告,添加js脚本 // @namespace _cus_ad_sp // @version 2.10.1 // @author vizo // @license MIT // @include /https?\:\/\/(?!greasyfork).*/ // @run-at document-start // @grant GM_addStyle // @grant GM_setValue // @grant GM_getValue // @grant GM_deleteValue // @grant GM_listValues // @grant GM_addElement // @grant GM_registerMenuCommand // @grant GM_setClipboard // @grant GM_xmlhttpRequest // @grant GM_download // @grant unsafeWindow // @connect * // @require https://unpkg.com/vue@2.6.14/dist/vue.min.js // @require https://unpkg.com/tiny-oss@0.5.1/dist/tiny-oss.min.js // @require https://unpkg.com/vio-utils@2.7.8/index.js // @require https://unpkg.com/@vizoy/tmk-utils@1.2.18/index.js // @noframes // @downloadURL none // ==/UserScript== Object.assign(TMK, vio) ;(function() { if (TMK.isMobile()) return let k = GM_getValue(`_cfg_${location.host}`) || {} k = typeof k === 'string' ? JSON.parse(k) : k if (k.css && !k.disCSS) { GM_addStyle(`sty9z1p52{}\n${k.css}\n`) } })(); tryAddGmSty() function tryAddGmSty() { const isAdd = document.head.querySelector('.sty777rx') if (!isAdd) { GM_addElement('style', { textContent: ` html body .dn8x { display: none !important; } html body .GM-Asd-yisi, html body .GM-Asd-certain { background-image: none !important; } html body .adsbygoogle { display: none !important; } .GM-Asd-yisi::before, .GM-Asd-certain::before { content: attr(fxkasd); width: 100%; height: 100%; font-size: 16px; color: #ddd; display: flex; justify-content: center; align-items: center; font-weight: normal; font-style: normal; font-family: Arial sans-serif; position: absolute; top: 0; left: 0; z-index: 1; } .GM-Asd-certain.imgRx, .GM-Asd-certain.ifrIx, img.GM-Asd-certain { visibility: hidden !important; overflow: hidden !important; height: 0 !important; } .GM-Asd-certain::before { content: attr(fxkasd); } .GM-Asd-certain.bdAsd::before { content: '百度广告' !important; } .GM-Asd-certain.gooAsd::before { content: '谷歌广告' !important; } .GM-Asd-certain.qrc7Box::before { content: '二维码'; } .GM-Asd-yisi > *, .GM-Asd-certain > * { visibility: hidden !important; opacity: 0 !important; } .GM-Asd-yisi:hover > *, .GM-Asd-certain:hover > * { visibility: visible !important; opacity: 0.8 !important; animation: anim5z 1.7s both; } html .r3el5tve { position: relative !important; } @keyframes anim5z { 0% { opacity: 0; } 30% { opacity: 0; } 100% { opacity: 0.8; } } @keyframes anim8z { 90% { visibility: visible; } 100% { opacity: 0; visibility: hidden; } } .GM-Asd-yisi:hover::before, .GM-Asd-certain:hover::before { animation: anim8z 1.5s both; } #wp5rh [hidden] { display: none !important; } #wp5rh, #wp5rh * { margin: 0; padding: 0; box-sizing: border-box !important; } #wp5rh { width: 28vw; height: 68vh; min-width: 400px; min-height: 400px; padding: 30px; background: #fff; border-radius: 3px; font-family: sans-serif,"HelveticaNeue",Helvetica,"PingFangSC","MicrosoftYaHei","HiraginoSansGB",Arial; line-height: 1.5; font-size: 12px; resize: both; box-shadow: 0 0 5px #ccc; position: fixed; top: 0; right: 0; bottom: 0; left: 0; z-index: 50050; margin: auto; } #wp5rh .mwp_5c { height: 100%; display: flex; flex-direction: column; position: relative; } #wp5rh .mwp_5c::before { content: '加载中...'; background: #fff9; font-size: 14px; color: #999; justify-content: center; align-items: center; position: absolute; top: 0; right: 0; bottom: 0; left: 0; z-index: 1; display: none; } #wp5rh .mwp_5c.ld::before { display: flex; } #wp5rh .tit1v { color: #555; font-size: 18px; text-align: center; margin-bottom: 15px; } #wp5rh .c7d-item { margin-bottom: 10px; display: flex; flex-direction: column; flex-wrap: wrap; } #wp5rh .allset-item { flex: 1; } #wp5rh .css-item-xh { flex: 3; transition: flex .3s; } #wp5rh .js-item-xh { flex: 1; transition: flex .3s; } #wp5rh .mwp_5c.expandJS .css-item-xh { flex: 1; } #wp5rh .mwp_5c.expandJS .js-item-xh { flex: 3; } #wp5rh .stiz { font-size: 14px; color: #555; margin-bottom: 3px; position: relative; text-align: left; display: flex; } #wp5rh .tamp-cfg-modal { width: 250px; height: 170px; padding: 10px; border-radius: 2px; background: #f1f1f1; position: absolute; top: -50px; right: 0; bottom: 0; left: 0; margin: auto; z-index: 2; } #wp5rh .tamp-cfg-modal .txa-cfg { width: 100%; height: 100%; resize: none; color: #777 !important; font-family: Consolas; font-size: 12px; padding: 6px; overflow-y: auto; border: 1px solid #e6e6e6; background: #fafafa; } #wp5rh .tamp-cfg-modal .txa-cfg::-webkit-input-placeholder { color: #ccc !important; } #wp5rh .stiz .s0l { flex: 1; } #wp5rh .stiz .s0r { color: #09e; cursor: pointer; user-select: none; margin-left: 10px; } #wp5rh .stiz .s0r.on { color: #9a9a9a; } #wp5rh .oss-zbtn { color: #c7c7c7; cursor: pointer; user-select: none; margin-right: 10px; } #wp5rh .view-all-set { color: #09e; cursor: pointer; user-select: none; } #wp5rh .st1k { display: flex; } #wp5rh .st1k .s1p { flex: 1; } #wp5rh .st1k .s2p { width: 65px; color: #09e; cursor: pointer; text-align: right; white-space: nowrap; overflow: hidden; } #wp5rh .inpy { flex: 0 0 auto; height: 32px; border: 1px solid #ddd; color: #555; background: #fff; border-radius: 2px; padding: 0 10px; outline: none; } #wp5rh .inpy:focus { border: 1px solid #c1c1c1; } #wp5rh .txtr1z { width: 100%; flex: 1; color: #555; padding: 6px; line-height: 1.4; overflow-x: hidden; overflow-y: auto; border-radius: 2px; border: 1px solid #ddd; background: #fff; font-size: 12px; resize: none; white-space: pre-line; outline: none; font-family: Consolas,sans-serif,"Helvetica Neue",Helvetica,"PingFang SC","Microsoft YaHei"; } #wp5rh .txtr1z::-webkit-input-placeholder { color: #c5c5c5; } #wp5rh .txtr1z:focus { border: 1px solid #39e; } #wp5rh .txtr1z.disabled { color: #999; background: #f5f5f5; } #wp5rh .txtr1z::-webkit-scrollbar { width: 4px; } #wp5rh .txtr1z::-webkit-scrollbar-corner, #wp5rh .txtr1z::-webkit-scrollbar-track { background-color: #fff; } #wp5rh .txtr1z::-webkit-scrollbar-thumb { background: #fff; } #wp5rh .txtr1z:hover::-webkit-scrollbar-thumb { background: #e1e1e1; } #wp5rh .txtr1z:hover::-webkit-scrollbar-corner, #wp5rh .txtr1z:hover::-webkit-scrollbar-track { background-color: #f7f7f7; } #wp5rh .btn-w { margin-top: 5px; display: flex; flex-direction: row-reverse; } #wp5rh .btn-w .c5kbtn { width: 90px; height: 32px; border-radius: 2px; margin-right: 10px; font-family: sans-serif,"Helvetica Neue",Helvetica,"PingFang SC","Microsoft YaHei" !important; cursor: pointer; outline: none; border: 0; } #wp5rh .btn-w .c5kbtn.b1 { color: #fff !important; background: #09e !important; } #wp5rh .btn-w .c5kbtn.b2 { color: #555 !important; background: #f1f1f1 !important; } #wp5rh .btn-w .c5kbtn:first-child { margin-right: 0; } #wp5rh .btn-w .c5kbtn:focus { border: 0; } #wp5rh .btn-w .c5kbtn:hover { opacity: 0.9; } `, class: 'sty777rx', }) } } const html = (s) => { return s[0] } const G = { hostIgnore: /\b(taobao|jd|tmall|bilibili)\.com|192\.|\bbaidu\.com\/s/i, ifrIgnore: /\b(github|qq|geetest|taobao|aliyundrive|163)\.com|\brecaptcha/i, linkIgnore: /\b(github|gitee)\.com/i, tmpTime: 0, html: html`
设置

打开面板快捷键 oss {{ viewSetText }}

添加css(不含style标签) {{ disCssText }}

添加js(不含script标签) {{ disJsText }}

已添加的网站(可删除) {{ addedNum }} 个 导入配置 导出配置

`, } const vm = new Vue({ data() { return { // 模态框状态 dialog1s: false, // 快捷键名称 eKey: '', showAllSet: false, // css代码 texCssVal: '', // js代码 texJsVal: '', // 已添加的网站 allAddedText: '', disCSS: false, disJS: false, // 是否展开js isExpandJS: false, isLoad: false, // oss配置modal isShowTampModal: false, tampCfgVal: '', } }, computed: { viewSetText() { return this.showAllSet ? '查看当前网站' : '查看全部网站' }, addedNum() { return this.allAddedText .split('\n') .filter(v => !!v) .length }, disCssText() { return this.disCSS ? '已禁用' : '禁用css' }, disJsText() { return this.disJS ? '已禁用' : '禁用js' }, }, watch: { eKey(nVal) { this.eKey = /[a-z\,]/.test(nVal) ? nVal : '' GM_setValue('_gus_keyboard', this.eKey) }, dialog1s(nVal) { if (!nVal) { this.showAllSet = false this.isShowTampModal = false } }, }, methods: { setGmVal(obj) { obj.firstTime = obj.firstTime ?? Math.trunc(Date.now() / 1e3) return GM_setValue(`_cfg_${location.host}`, obj) }, getGmVal() { let gmVal = GM_getValue(`_cfg_${location.host}`) || {} return typeof gmVal === 'string' ? JSON.parse(gmVal) : gmVal }, hdlTgOssModal() { this.isShowTampModal = !this.isShowTampModal }, hdlExpandJSJs(type) { this.isExpandJS = type === 2 }, changeTampCfg() { GM_setValue('tampOssCfg7n', this.tampCfgVal) }, async hdlSave() { if (!this.showAllSet) { this.saveCssAndJs() this.initAddedWebToTextArea() } else { this.updateTextAreaValToGm() await this.saveJsonToOss('state') await this.saveJsonToOss('cfg') location.reload() } this.dialog1s = false }, hdlCancel() { this.dialog1s = false }, // 禁用css hdlTgDisCss() { this.disCSS = !this.disCSS }, // 禁用js hdlTgDisJs() { this.disJS = !this.disJS }, async saveCssAndJs() { let css = `` document.head.insertAdjacentHTML('beforeend', css) Array.from(document.querySelectorAll('.sty9z1p52')).forEach((v, i, y) => { if (i !== y.length - 1) { v.remove() } else { v.disabled = this.disCSS } }) const gm = this.getGmVal() this.setGmVal({ ...gm, css: this.texCssVal, js: this.texJsVal, disCSS: this.disCSS, disJS: this.disJS, }) if (!this.texCssVal && !this.texJsVal) { GM_deleteValue(`_cfg_${location.host}`) } // 同步至oss if ( !await this.saveJsonToOss('state') ) { alert('同步失败, 请导出配置后在其他网站重新导入配置就能同步了') return } await this.saveJsonToOss('cfg') if ( gm.disJS !== this.disJS || gm.js !== this.texJsVal || !this.texCssVal.trim() && !this.texJsVal.trim() ) { if (this.disJS) { localStorage.removeItem('TMK_INIT_FUNC') } await TMK.timeout(500) location.reload() } }, saveJsonToOss(name) { let gmCfg = GM_getValue('tampOssCfg7n') if (!gmCfg) return Promise.resolve(true) gmCfg = typeof gmCfg === 'string' ? JSON.parse(gmCfg) : gmCfg const oss = new TinyOSS(gmCfg.ossParams) const lastTime = Date.now() const data = name === 'state' ? { lastTime } : this.getAllCfg() const blob = new Blob([JSON.stringify(data)], { type: 'text/json' }) const date = TMK.fmt(Date.now(), 'Y-M-D') GM_setValue('tampCfgUpdateTime', lastTime) return new Promise(async (resolve) => { try { if (name === 'cfg') { oss.put(`json/tamp-cfg/cus-cssjs/${date}/${name}.json`, blob) await TMK.timeout(300) } await oss.put(`json/tamp-cfg/cus-cssjs/1-cfg/${name}.json`, blob, { onprogress(e) { if (e.total > 0) { return resolve(true) } } }) } catch (err) { return resolve(false) } }) }, GM_req(url) { return new Promise((resolve, reject) => { GM_xmlhttpRequest({ url, method: 'get', responseType: 'json', onload: function(xhr) { resolve(xhr.response) }, }) }) }, async updateCfgFromOss() { const gmCfg = this.getOssCfg() const gmLastTime = GM_getValue('tampCfgUpdateTime') if (gmCfg) { try { const url1 = `${gmCfg.state}&t=${Date.now()}` const url2 = `${gmCfg.cfg}&t=${Date.now()}` const res = await this.GM_req(url1) const { lastTime } = res const now = Date.now() if ( !gmLastTime || lastTime > gmLastTime || now - gmLastTime > 3e4 ) { const rCfg = await this.GM_req(url2) this.updateJsonToGm(rCfg) // 这里也初始化一次 this.initAddedWebToTextArea() GM_setValue('tampCfgUpdateTime', now) } } catch (e) { TMK.log( e.message ) } } }, // 判断是否从远程更新 tryUpdateCfgFromOss() { const gmTime = GM_getValue('pageRefreshTime') const now = Date.now() if (!gmTime || now - gmTime > 5000) { GM_setValue('pageRefreshTime', now) return new Promise(async (resolve) => { this.isLoad = true await this.updateCfgFromOss() this.isLoad = false resolve() }) } }, updateTextAreaValToGm() { const gmArr = GM_listValues() .filter(v => v.startsWith('_cfg_')) .map(v => v.replace(/^_cfg_/, '')) const cArr = this.allAddedText.split('\n') gmArr.forEach(v => { if (!cArr.some(c => v === c)) { GM_deleteValue(`_cfg_${v}`) } }) }, updateJsonToGm(obj) { if (!obj) return GM_listValues() .filter(v => /^_gus_|^_cfg_/.test(v)) .forEach(v => { GM_deleteValue(v) }) // 初始化firstTime for (let i in obj) { if (!obj[i].firstTime) { obj[i].firstTime = Math.trunc(Date.now() / 1e3) } } for (let k in obj) { GM_setValue(k, obj[k]) } }, tgCfgDialog() { this.dialog1s = !this.dialog1s }, hdlViewAllSet() { this.showAllSet = !this.showAllSet }, // 导入配置 hdlImportCfg() { this.$refs.inp_hide.click() }, // 导出配置 hdlExportCfg() { const obj = this.getAllCfg() TMK.downloadText(JSON.stringify(obj, null, 2), '1.json') }, // 获取oss配置 getOssCfg() { let gmCfg = GM_getValue('tampOssCfg7n') if (!gmCfg) return try { return typeof gmCfg === 'string' ? JSON.parse(gmCfg) : gmCfg } catch (err) {} }, initOssVal() { const gmCfg = this.getOssCfg() if (gmCfg) { this.tampCfgVal = JSON.stringify(gmCfg) } }, // 获取所有已配置的网站数据 getAllCfg() { const obj = GM_listValues() .filter(v => /^_gus_|^_cfg_/.test(v)) .reduce((acc, v) => { let gmVal = GM_getValue(v) gmVal = typeof gmVal === 'string' && gmVal > 1 ? JSON.parse(gmVal) : gmVal return { ...acc, [v]: gmVal } }, {}) for (let k in obj) { if (k.startsWith('_cfg_')) { if (!obj[k]?.css.trim() && !obj[k]?.js.trim()) { Reflect.deleteProperty(obj, k) } } } return obj }, hdlUpFile(e) { let file = e.target.files[0] if (file) { let reader = new FileReader() reader.readAsText(file, 'utf-8') reader.onload = async (evt) => { try { oUp = JSON.parse(evt.target.result) this.updateJsonToGm(oUp) if (!await this.saveJsonToOss('state')) { alert('上传失败, 请选择其他网站重新上传') return } await this.saveJsonToOss('cfg') this.initCssJsVal() this.initAddedWebToTextArea() this.initAddedScript() this.initGMStyleClass() setTimeout(() => { location.reload() }, 200) } catch (e) { // 上传失败 } } } }, async resetCss() { this.texCssVal = '' this.disCSS = false this.setGmVal({ ...this.getGmVal(), css: '', disCSS: false, }) await this.saveJsonToOss('state') await this.saveJsonToOss('cfg') location.reload() }, async resetJs() { this.texJsVal = '' this.disJS = false this.setGmVal({ ...this.getGmVal(), js: '', disJS: false, }) await this.saveJsonToOss('state') await this.saveJsonToOss('cfg') location.reload() }, initEvt() { window.addEventListener('keydown', e => { const el = e.target const edt = el.getAttribute('contenteditable') const unEdt = edt !== 'true' && edt !== '' if ( !(/text|search|number|password|tel|url|email/.test(el.type)) && el.tagName !== 'TEXTAREA' && unEdt && !e.altKey && !e.ctrlKey && this.eKey.split(',').map(v => v.trim()).includes(e.key) ) { this.tgCfgDialog() } if (/esc/i.test(e.key)) { this.dialog1s = false } }) }, initEkey() { this.eKey = GM_getValue('_gus_keyboard') || '' }, initCssJsVal() { this.texCssVal = this.getGmVal().css || '' this.texJsVal = this.getGmVal().js || '' this.disCSS = !!this.getGmVal().disCSS this.disJS = !!this.getGmVal().disJS }, initAddedWebToTextArea() { const sorted = this.sortSiteList(GM_listValues()) const nArr = sorted .filter(v => v.startsWith('_cfg_')) .map(v => v.replace(/^_cfg_/, '')) this.allAddedText = nArr.join('\n') }, initAddedScript() { let js = this.getGmVal().js let isDisabled = this.getGmVal().disJS if (js && !isDisabled) { GM_addElement('script', { type: 'module', textContent: js, }) } }, // 网站列表排序, 按时间倒序排列 sortSiteList(siteList) { if (!siteList.length) return [] const nArr = siteList.map(v => { return { name: v, t: GM_getValue(v)?.firstTime, } }) .sort((a, b) => { return a.t - b.t > 0 ? -1 : 0 }) .map(v => v.name) return nArr }, initGMStyleClass() { document.querySelectorAll('style').forEach(v => { if (!/\w{8}(-\w{4}){3}-\w{12}/.test(v.id)) { return } if (v.textContent.includes('sty9z1p52')) { v.classList.add('sty9z1p52') } }) }, }, async mounted() { this.initEvt() this.initOssVal() this.initEkey() await this.tryUpdateCfgFromOss() this.initAddedWebToTextArea() this.initCssJsVal() this.initAddedScript() this.initGMStyleClass() }, }) function tryAppendWp5() { const wp5 = document.getElementById('wp5rh') if (!wp5) { document.body.insertAdjacentHTML('beforeend', G.html) } } // MKS // -- 规则 start gvz ----------------- function isIgnHost() { return G.hostIgnore.test(location.host + location.pathname) } function isIgnLink(linkUrl) { try { const lnk = new URL(linkUrl.replace(/^\/\//, 'https://')) return G.linkIgnore.test(lnk.host + lnk.pathname) } catch { return false } } function isIgnIfr(url) { try { const ifr = new URL(url.replace(/^\/\//, 'https://')) return G.ifrIgnore.test(ifr.host + ifr.pathname) } catch { return false } } // 是否是广告商 function isAdvertiser(str) { return /\b(pos\.baidu|google(sy|ad)|mediav)|adsbygoogle|adx\.php/.test(str) } function isBlank(el) { return /_blank/i.test(el.target) } function isHtmlBody(el) { return /^(body|html)$/i.test(el.nodeName) } function imgSrcYsAd(src) { if (!src || src.includes('data:image/')) { return false } const str = src.slice(0, 80) + src.slice(-120) return /(\b|_)(ads?|close|adve\w+)[-_]?\d*\.(png|jpg|gif|webp)/i.test(str) } function adTxt(s) { if (!s) return false return /([^\u4e00-\u9fa5]|\b\^|$)广告([^\u4e00-\u9fa5]|\b\^|$)/.test(s.slice(0, 400)) } // sgn function hasAdSign(el) { const adm = (s) => { if (typeof s !== 'string' || s.includes('data:image/')) { return false } s = s.slice(0, 80) + s.slice(-120) return ( /(\b|_)ads?(ver)?[-_]?\d{0,10}(\b|_)|Adver|(\b|_)ads?[_-]\w+/i.test(s) || /(\b|_)ad[A-Z][a-z]{2,6}\d{0,4}\b|[a-z]{4}Ad(\b|_)|(\b|_)sinaad|topAd/.test(s) || isAdvertiser(s) ) && !hasUUID(s) } const attArr = [...el.attributes].filter(v => v.nodeName !== 'style') return adTxt(el.title) || adTxt(el.alt) || adTxt(el.textContent) || hasAdTextInBeAf(el) || attArr.some(v => adm(v.value) || adTxt(v.value)) } function hasSibling(el) { return TMK.getSiblings(el).length > 1 } function isGif(src) { return /(\.gif)|^data:image\/gif;/i.test(src) } function ysGifAd(el) { const lazy = attr(el, 'data-src') const src = attr(el, 'src') return isGif(src) && !lazy && !isSwiper(el.parentElement) && likeAdSize(el) && !isSmallSize(el) } function lnkEqImgUrl(link, imgUrl) { return link === imgUrl } function isAbs(url) { return /^https?|^\/\//i.test(url) } function isSwiper(el) { const pEl = el.parentElement const gEl = pEl.parentElement const inc = (ex) => { const p = attr(ex, 'class') return p ? p.includes('swiper') : false } return inc(el) || inc(pEl) || inc(gEl) } // 大小是否符合广告的尺寸(面积) function likeAdSize(el) { if (!TMK.isVisible(el)) { return false } return ( !isSmallSize(el) && !isLargerSize(el) && bcr(el).height < 710 ) } function isLargerSize(el) { return bcr(el).width * bcr(el).height >= 800 * 550 } function isSmallSize(el) { const w = bcr(el).width const h = bcr(el).height return ( (w * h) <= (80 * 40) || w < 65 || h < 30 ) && w > 0 && h > 0 } function siblingHasAd(el) { const sib = TMK.getSiblings(el) if (sib.length === 0 || sib.length > 10) { return false } const adm = (s) => { return /(\b|_)close[-_]?\d{0,5}(\b|_)/.test(s?.slice(-120)) } return sib.some(v => { return hasAdSign(v) && !!v.childNodes.length }) } // 判断是否在页面边角吗角落 function isCorner(el) { if (!TMK.isVisible(el)) { return false } const dcW = document.body.clientWidth const crz = bcr(el) const elX = crz.x const elW = crz.width const elH = crz.height const elT = crz.top return ( elX - 40 <= 0 || elX + elW + 40 >= dcW ) && elW > 0 && elH > 0 && elT > 90 } // 靠近顶部 function isNearTop(el) { const y = bcr(el).top const ht = bcr(document.documentElement).top return y >= 0 && y <= 65 && ht > -10 && bcr(el).width > 30 && bcr(el).height > 30 } function likeLogo(el) { return isNearTop(el) && bcr(el).left < 800 && bcr(el).width < 330 } function hasUUID(str) { return /[a-f\d]{4}(?:[a-f\d]{4}-){4}[a-f\d]{12}/i.test(str) } function hasScript(el) { return el.querySelectorAll('script').length > 0 } function hasIframe(el) { return el.querySelectorAll('iframe').length > 0 } function hasAdTextInBeAf(el) { const bf = (str) => { const cnt = getComputedStyle(el, str).getPropertyValue('content') return adTxt(cnt) } return bf('::before')|| bf('::after') } function likeQrcSize(el) { const iw = bcr(el).width const ih = bcr(el).height return iw > 80 && ih > 80 && iw < 385 && ih < 390 && iw / ih > 0.7 && iw / ih < 1.1 } function isFxk(el) { const arr = TMK.getParentEls(el) return !!attr(el, 'fxkasd') || arr.some(v => !!attr(v, 'fxkasd')) } // 宽高比例疑似 function ysAdRatio(el) { const w = bcr(el).width const h = bcr(el).height return (w / h) > 3.6 && w > 30 && h > 30 } function biggerRatio(el) { const w = bcr(el).width const h = bcr(el).height return (w / h) > 10 && w > 30 && h > 30 } function ifrHasIfr(el) { try { const doc = el.contentWindow?.document return [...doc.querySelectorAll('iframe')].length > 0 } catch { return false } } // MKS // - 规则 end------------------------------- function bcr(el) { return el.getBoundingClientRect() } function setFxK(el, str) { el.setAttribute('fxkasd', str.toUpperCase()) } function attr(el, p) { return el ? el.getAttribute(p) : '' } // 内联a标签转换为inline-block function setInlineBlock(link) { if (getComputedStyle(link).display === 'inline') { link.style.display = 'inline-block' } } // 如果是static则设置为relative function setEleAsRelative(el) { if (getComputedStyle(el).position === 'static') { el.style.position = 'relative' el.classList.add('r3el5tve') } } function linkHost(url) { return TMK.isUrl(url) ? new URL(url).host : null } function compareTwoBox(el1, el2) { const w = bcr(el1).width const h = bcr(el1).height const pw = bcr(el2).width const ph = bcr(el2).height return 40 >= Math.abs(pw - w) && 30 >= Math.abs(ph - h) && pw > 30 && ph > 15 } function setCertainCls(el, cls = '') { const hasFk = (fk) => { return !!el?.src?.includes(fk) || [...el.querySelectorAll('iframe')]?.some(ifr => ifr?.src?.includes(fk)) } const bdCls = hasFk('pos.baidu') ? 'bdAsd' : '' const ggCls = hasFk('googlead') ? 'gooAsd' : '' const clsStr = `GM-Asd-certain ${bdCls} ${ggCls} ${cls}`.trim().replace(/\s{2,}/, ' ').split(' ') el.classList.add(...clsStr) } // 删除添加的屏蔽class和标记 function removeAsdMk(el, isRecursive) { let dArr = [el] if (isRecursive) { dArr = [...dArr, ...TMK.getParentEls(el)] } dArr.forEach(v => { v.classList.remove( 'GM-Asd-yisi', 'GM-Asd-certain', 'bdAsd', 'gooAsd', 'qrc7Box', ) v.removeAttribute('fxkasd') }) } function imgIsLargeSize(img) { if (img.nodeName !== 'IMG') { return false } return isLargerSize(img) } function trySetMkRecursive(el, mk1, mk2, clsStr = '') { if (isFxk(el)) { return } const zArr = [...[el], ...TMK.getParentEls(el)].reverse() const kArr = zArr.filter(v => { return bcr(v).width > 50 && bcr(v).height > 35 }) const ysSubNav = zArr.length - kArr.length >= 4 const laEl = kArr[kArr.length - 1] let isAdd = false for (let pl of kArr) { // 避免Dom嵌套过深 if (kArr.length > 8 || ysSubNav) { break } if (compareTwoBox(pl, laEl) && pl !== el) { if ( !isHtmlBody(pl) && ( likeAdSize(pl) || imgIsLargeSize(el) ) ) { setEleAsRelative(pl) setCertainCls(pl, clsStr) setFxK(pl, mk1) isAdd = true } break } } el.classList.add('x8x') const ipl = el.parentElement if ( !isAdd && likeAdSize(el) && !isHtmlBody(el) ) { setEleAsRelative(el) setCertainCls(el, clsStr) setFxK(el, mk2) } else if ( !isHtmlBody(ipl) && ( !isAdd || imgIsLargeSize(el) ) ) { setEleAsRelative(ipl) setCertainCls(ipl, clsStr + ' imgRx') setFxK(ipl, mk2) } } // 屏蔽body直接子元素疑似AD bda function blockBodyCdYsAd() { document.querySelectorAll('body > a').forEach(el => { const cs = getComputedStyle const posz = cs(el).position === 'fixed' && (cs(el).zIndex > 90 || cs(el).zIndex === 'auto') const isLnk = el.tagName === 'A' if (isLnk && isLargerSize(el)) { el.classList.add('dn8x') el.style.display = 'none' return } }) } // 屏蔽div中含有script的疑似AD scp function blockScriptInDiv() { document.querySelectorAll('script').forEach(el => { const pEl = el.parentElement if (isLargerSize(pEl)) { removeAsdMk(el, true) return } if (isFxk(pEl)) { return } if ( ( hasAdSign(el) || hasAdSign(pEl) || siblingHasAd(el) ) && likeAdSize(pEl) ) { trySetMkRecursive(pEl, 's05', 's98') } }) } // 屏蔽疑似ad的box sltad function blockYsSlt() { const sArr = [ 'div[class*="ad" i]', 'div[id*="ad" i]', 'ins[class*="ad" i]', 'ins[id*="ad" i]', 'div[aria-label*="ad" i]', ] document.querySelectorAll(sArr.join(',')).forEach(el => { if (isLargerSize(el)) { removeAsdMk(el, true) return } if (isFxk(el)) { return } if ( hasAdSign(el) && likeAdSize(el) ) { trySetMkRecursive(el, 's18', 's85') } }) } // 屏蔽iframe广告 ifs function blockIfrAd() { document.querySelectorAll('iframe').forEach(v => { const src = v?.src if (isIgnIfr(src)) { return } if (isLargerSize(v)) { removeAsdMk(v, true) return } const pEl = v.parentElement const rLen = () => { return [ hasAdSign(v), hasAdSign(pEl), ysAdRatio(v), ].filter(v => !!v).length } if ( rLen() >= 1 || ( ifrHasIfr(v) && ysAdRatio(v) ) || ( isNearTop(v) && ysAdRatio(v) ) ) { trySetMkRecursive(v, 'f03', 'f95', 'ifrIx') } }) } function blockYsVideoAd() { document.querySelectorAll('video').forEach(vdo => { const rLen = () => { return [ hasAdSign(vdo), ] .filter(v => !!v) .length } if ( likeAdSize(vdo) && rLen() >= 1 || ysAdRatio(vdo) ) { if (!isFxk(vdo)) { setCertainCls(vdo, 'vdoRb') setFxK(vdo, 'v88') } } }) } // ims function blockYsImgInLink() { Array.from(document.querySelectorAll('a > img')).forEach(el => { const link = el.parentElement const pEl = link.parentElement const lnkUrl = attr(link, 'href') if (isIgnLink(lnkUrl)) { return } const rArrLen = () => { return [ isBlank(link), ysGifAd(el), imgSrcYsAd(attr(el, 'src')), hasAdSign(link), hasAdSign(pEl), hasAdSign(el), siblingHasAd(link), siblingHasAd(el), isNearTop(el), biggerRatio(el), ].filter(v => !!v).length } if ( rArrLen() >= 2 && !isSmallSize(el) && !likeLogo(el) ) { trySetMkRecursive(el, 'i05', 'i98') } }) } function wchDom() { TMK.watchDom('body', TMK.throttle(() => { tryAddGmSty() }, 500)) } function regMainEvt() { ['mousemove', 'scroll'].forEach(evt => { window.addEventListener(evt, insMainAdFn) }) } function insMainAdFn() { let now = Date.now() if (now - G.tmpTime > 1500) { G.tmpTime = now if (!isIgnHost()) { // 链接下的图片 blockYsImgInLink() blockBodyCdYsAd() blockYsSlt() // 确认iframe广告 blockIfrAd() blockScriptInDiv() // 视频广告 blockYsVideoAd() } } } function initTimer() { insMainAdFn() setTimeout(initTimer, 8000) } async function initFunc() { if (!TMK.isMobile()) { const funcStr = localStorage.getItem('TMK_INIT_FUNC') if (funcStr) { try { TMK.evalFunc(funcStr, TMK) } catch(e) { TMK.log( e.message ) } } setTimeout(() => { if (!isIgnHost()) { regMainEvt() } }, 5e3) await TMK.waitDomLoad() tryAppendWp5() vm.$mount('#wp5rh') GM_registerMenuCommand('打开设置面板', vm.tgCfgDialog) GM_registerMenuCommand('清空当前网站添加的CSS', vm.resetCss) GM_registerMenuCommand('清空当前网站添加的JS', vm.resetJs) initTimer() wchDom() } } initFunc()