Skip to content

Instantly share code, notes, and snippets.

@QETHAN
Created July 7, 2014 02:20
Show Gist options
  • Save QETHAN/03c2604ac4901aa9d0e2 to your computer and use it in GitHub Desktop.
Save QETHAN/03c2604ac4901aa9d0e2 to your computer and use it in GitHub Desktop.
视频广告屏蔽插件
if(!document.URL.match(new RegExp('v\\.baidu\\.com'))){
(function() {
Function.prototype.bind = function() {
var fn = this, args = Array.prototype.slice.call(arguments), obj = args.shift();
return function() {
return fn.apply(obj, args.concat(Array.prototype.slice.call(arguments)));
};
};
function A() {}
A.prototype = {
rules: {
'youku_loader': {
'find': /^http:\/\/static\.youku\.com\/.*(loader|player_.*)(_taobao)?\.swf/,
'replace': 'http://swf.adtchrome.com/loader.swf'
},
'youku_out': {
'find': /^http:\/\/player\.youku\.com\/player\.php\/sid\/(.*)/,
'replace': 'http://swf.adtchrome.com/loader.swf?VideoIDS=$1'
},
'pps':{
'find':/^http:\/\/www\.iqiyi\.com\/player\/cupid\/common\/pps_flvplay_s\.swf/,
'replace':'http://swf.adtchrome.com/pps_20140420.swf'
},
'iqiyi': {
'find':/^http:\/\/www\.iqiyi\.com\/player\/\d+\/Player\.swf$/,
'replace':'http://swf.adtchrome.com/iqiyi_20140624.swf'
},
'ku6':{
'find':/^http:\/\/player\.ku6cdn\.com\/default\/.*\/\d+\/(v|player|loader)\.swf/,
'replace':'http://swf.adtchrome.com/ku6_20140420.swf'
},
'ku6_topic':{
'find':/^http:\/\/player\.ku6\.com\/inside\/.*\/v\.swf/,
'replace':'http://swf.adtchrome.com/ku6_20140420.swf'
}
},
_done: null,
get done() {
if(!this._done) {
this._done = new Array();
}
return this._done;
},
addAnimations: function() {
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = 'object,embed{\
-webkit-animation-duration:.001s;-webkit-animation-name:playerInserted;\
-ms-animation-duration:.001s;-ms-animation-name:playerInserted;\
-o-animation-duration:.001s;-o-animation-name:playerInserted;\
animation-duration:.001s;animation-name:playerInserted;}\
@-webkit-keyframes playerInserted{from{opacity:0.99;}to{opacity:1;}}\
@-ms-keyframes playerInserted{from{opacity:0.99;}to{opacity:1;}}\
@-o-keyframes playerInserted{from{opacity:0.99;}to{opacity:1;}}\
@keyframes playerInserted{from{opacity:0.99;}to{opacity:1;}}';
document.getElementsByTagName('head')[0].appendChild(style);
},
animationsHandler: function(e) {
if(e.animationName === 'playerInserted') {
this.replace(e.target);
}
},
replace: function(elem) {
if(this.done.indexOf(elem) != -1) return;
this.done.push(elem);
var player = elem.data || elem.src;
if(!player) return;
var i, find, replace = false;
for(i in this.rules) {
find = this.rules[i]['find'];
if(find.test(player)) {
replace = this.rules[i]['replace'];
if('function' === typeof this.rules[i]['preHandle']) {
this.rules[i]['preHandle'].bind(this, elem, find, replace, player)();
}else{
this.reallyReplace.bind(this, elem, find, replace)();
}
break;
}
}
},
reallyReplace: function(elem, find, replace) {
elem.data && (elem.data = elem.data.replace(find, replace)) || elem.src && ((elem.src = elem.src.replace(find, replace)) && (elem.style.display = 'block'));
var b = elem.querySelector("param[name='movie']");
this.reloadPlugin(elem);
},
reloadPlugin: function(elem) {
var nextSibling = elem.nextSibling;
var parentNode = elem.parentNode;
parentNode.removeChild(elem);
var newElem = elem.cloneNode(true);
this.done.push(newElem);
if(nextSibling) {
parentNode.insertBefore(newElem, nextSibling);
} else {
parentNode.appendChild(newElem);
}
},
init: function() {
var handler = this.animationsHandler.bind(this);
document.body.addEventListener('webkitAnimationStart', handler, false);
document.body.addEventListener('msAnimationStart', handler, false);
document.body.addEventListener('oAnimationStart', handler, false);
document.body.addEventListener('animationstart', handler, false);
this.addAnimations();
}
};
new A().init();
})();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment