Skip to content

Instantly share code, notes, and snippets.

@myfreeer
Last active November 8, 2016 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myfreeer/57e9973ebe5398a246de23c152ff1d05 to your computer and use it in GitHub Desktop.
Save myfreeer/57e9973ebe5398a246de23c152ff1d05 to your computer and use it in GitHub Desktop.
just a dev example
// ==UserScript==
// @name canClearAd Element Fucker
// @namespace myfreeer
// @version 1.3
// @description Remove or replace some unwanted element
// @author myfreeer, ineer
// @include *
// @downloadURL https://gist.github.com/myfreeer/57e9973ebe5398a246de23c152ff1d05/raw/canClearAd.user.js
// @license MIT
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
(function() {
'use strict';
const webRuleUrl = 'https://ineer.github.io/canClearAd-rules/webRule.json',
clearRuleUrl = 'https://ineer.github.io/canClearAd-rules/rules/';
let removeRule = JSON.parse(GM_getValue('removeRule', '{"common":[]}')),
replaceRule = JSON.parse(GM_getValue('replaceRule', '{"common":[]}')),
lastUpdated = parseInt(GM_getValue('lastUpdated', 0)),
errorLog = [],
clearNum = 0,
finish = [0, 0];
if (Date.parse(new Date()) - lastUpdated > 1000 * 60 * 60 * 24) {
try {
fetch(webRuleUrl).then(res => res.json().then(json => json.urls.map(url => fetch(clearRuleUrl + url + '.json').then(res => res.json().then(json => {
removeRule[url] = json.removeRule;
replaceRule[url] = json.replaceRule;
GM_setValue('removeRule', JSON.stringify(removeRule));
GM_setValue('replaceRule', JSON.stringify(replaceRule));
GM_setValue('lastUpdated', Date.parse(new Date()));
finish[0] += 1;
})))));
fetch(clearRuleUrl + 'common.json').then(res => res.json().then(json => {
removeRule.common = json.removeRule;
replaceRule.common = json.replaceRule;
GM_setValue('removeRule', JSON.stringify(removeRule));
GM_setValue('replaceRule', JSON.stringify(replaceRule));
GM_setValue('lastUpdated', Date.parse(new Date()));
finish[1] += 1;
}));
let interval = setInterval(() => {
if (finish[0] > 0 && finish[1] > 0) {
GM_setValue('removeRule', JSON.stringify(removeRule));
GM_setValue('replaceRule', JSON.stringify(replaceRule));
console.log('canClearAd rule Updated!', replaceRule, removeRule);
GM_setValue('lastUpdated', Date.parse(new Date()));
clearInterval(interval);
}
}, 5000);
} catch (e) {
errorLog.push(e);
}
}
//console.log('canClearAd rule:', replaceRule, removeRule);
for (let i in removeRule)
if (location.href.match(i) && removeRule[i].length > 0) clearNum += removeAd(removeRule[i]);
for (let i in replaceRule)
if (location.href.match(i) && replaceRule[i].length > 0) clearNum += replaceAd(replaceRule[i]);
clearNum += removeAd(removeRule.common);
clearNum += replaceAd(replaceRule.common);
if (window.location.href.indexOf('iqiyi') > -1) setInterval(jumpAds, 1000);
if (errorLog.length > 0) console.warn('canClearAd error:', errorLog);
//console.log('canClearAd cleared ads:', clearNum);
})();
/*!
* canClearAd v0.1.0
* Copyright 2016 Ineer
* Licensed under MIT (https://github.com/ineer/canclearad/blob/master/LICENSE)
*/
function removeAd(rule = []) {
let clearNum = 0;
for (let i in rule) {
let ad = document.querySelectorAll(rule[i][0]);
if (ad)
for (let j in ad) {
ad[j] = getParent(ad[j], rule[i][1]);
ad[j].parentNode.removeChild(ad[j]);
clearNum++;
}
}
return clearNum;
}
/*
* replaceAd(),替换广告的函数。
* 遇到清除广告会导致位置错乱的可以将规则移至替换广告的规则中,
* 原理是根据广告规则匹配到相应的广告对象,并创建一个透明的的div块级标签,
* 替换原先的广告对象。
**/
function replaceAd(rule = []) {
let clearNum = 0;
for (let i in rule) {
let ad = document.querySelectorAll(rule[i][0]);
if (ad)
for (let j in ad) {
ad[j] = getParent(ad[j], rule[i][1]);
var div = document.createElement('div');
div.style.width = ad[j].offsetWidth + 'px';
div.style.height = ad[j].offsetHeight + 'px';
div.style.float = 'left';
ad[j].parentNode.replaceChild(div, ad[j]);
clearNum++;
}
}
return clearNum;
}
function getParent(obj, num = 0) {
if (!obj) return false;
return num > 0 ? getParent(obj.parentNode, num - 1) : obj;
}
function jumpAds() {
let temp_video = document.querySelectorAll('video');
for (let i in temp_video)
if (temp_video[i] && temp_video[i].src.match('data.video.qiyi.com/videos/other/')) temp_video[i].playbackRate = 10;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment