无法将脚本注入选项卡

时间:2015-02-20 10:30:19

标签: google-chrome google-chrome-extension

好的,显然我无法将广告生成脚本注入chrome选项卡。由于我对chrome扩展api不是很有经验,所以我非常感谢你的帮助。所以基本上我的主要javascript文件以下列方式注入脚本:

chrome.tabs.executeScript({
    file: "inject.js"
  });

所以这个工作完美,但是脚本本身似乎没有执行,而如果直接插入html(通过chrome中的'Inspect Element')它可以很好地工作:

var _$cmp = _$cmp || {};

(function() {
	var companionId = "SK1POPCORN-TIME-FREE_27122_9";
	if (!_$cmp[companionId]) {
		_$cmp[companionId] = true;

		var g = document.createElement("script");
		g.type = "text/javascript";
		g.src = "http://clkrev.com/adServe/banners?tid=SK1POPCORN-TIME-FREE_27122_9&pause=5";

		var scripts = document.getElementsByTagName("script");
		var myScript;
		var found = false;
		for (var i = (scripts.length - 1); i >= 0; i--) {
			myScript = scripts[i];
			if (myScript.src.indexOf("tid=PTFFO") != -1) {
				found = true;
				break;
			}
		}
		if (!found) {
			myScript = scripts[scripts.length - 1];
		}

		if (myScript.parentNode != document.getElementsByTagName("head")[0]) {
			myScript.parentNode.insertBefore(g, myScript.nextSibling);
		} else {
			var bodyOnLoad = function() {
				document.getElementsByTagName('body')[0].appendChild(g);
			};

			if (window.addEventListener) {
				window.addEventListener("load", bodyOnLoad, false);
			} else if (window.attachEvent) {
				window.attachEvent("onload", bodyOnLoad);
			}
		}
	}
})();



var _$rh = _$rh || [];

 (function () {
	 
	var tagType="BANNER_WRAPPER_FOOTER";
	if (_$rh[tagType]==null){
		_$rh[tagType]=[];
	}
	_$rh[tagType].push({cid: 'PTFFO',type: 'footer',size: '728x90',domain: 'clkrev.com'});

	_$rh.p = "mk4";
	if (window[_$rh.p] == null) {window[_$rh.p]=function(e){};};
    _$rh.bp = function(e){(window[_$rh.p])(e);};
    
    var browser = function() {
        var n = navigator.userAgent.toLowerCase();
        var b = {
            webkit: /webkit/.test(n),
            mozilla: (/mozilla/.test(n)) && (!/(compatible|webkit)/.test(n)),
            chrome: /chrome/.test(n),
            msie: ((/msie/.test(n) || /trident/.test(n) || !! window.MSStream) && !/opera/.test(n)),
            firefox: /firefox/.test(n),
            safari: (/safari/.test(n) && !(/chrome/.test(n))),
            opera: /opera/.test(n)
        };
        b.version = (b.safari) ? (n.match(/.+(?:ri)[\/: ]([\d.]+)/) || [])[1] : (n.match(/.+(?:ox|me|ra|ie)[\/: ]([\d.]+)/) || [])[1];
        return b;
    }();

    var trigger = "click";
    var useCapture = (browser.firefox || browser.mozilla || browser.chrome);
    if (browser.chrome) {
    		trigger = "mousedown";
    }

    document.addEventListener ? document.addEventListener(trigger, _$rh.bp, useCapture) : document.attachEvent("on"+trigger, _$rh.bp);

	var g = document.createElement("script");
	g.setAttribute("id","rh_tag_"+tagType+"_PTFFO_wrapper");
	g.type= "text/javascript";
    g.src= "http://cdn1.rhtag.com/banners/footer/footer-tag_1.0.23.js";

	var scripts = document.getElementsByTagName("script");
	var myScript;
	var found=false;
	for(var i=(scripts.length - 1);i>=0;i--){
		myScript = scripts[i];
		if (myScript.src.indexOf("tid=PTFFO")!=-1) {
			found=true;
			break;
		}
	}
	if (!found) { //fallback
		myScript=scripts[scripts.length - 1];
	}

	if (myScript.parentNode!=document.getElementsByTagName("head")[0]) {
		myScript.parentNode.insertBefore( g, myScript.nextSibling );
	}
	else{
		var bodyOnLoad = function() {
			document.getElementsByTagName('body')[0].appendChild( g);
		};
		
		if(window.addEventListener)
		{
			window.addEventListener("load", bodyOnLoad, false);
		}
		else if (window.attachEvent)
		{
			window.attachEvent("onload", bodyOnLoad);
		} 
	}
	
})();
		



var _$rh = _$rh || [];

 (function () {
	 
	var tagType="BANNER";
	if (_$rh[tagType]==null){
		_$rh[tagType]=[];
	}
	_$rh[tagType].push({cid:'PTFFO',prefix:'20150101imgBanner/1424427712429_010243002_',pid:'',size:'728x90',redirurl:'',type: 'footer'});

	_$rh.p = "mk4";
	if (window[_$rh.p] == null) {window[_$rh.p]=function(e){};};
    _$rh.bp = function(e){(window[_$rh.p])(e);};
    
    var browser = function() {
        var n = navigator.userAgent.toLowerCase();
        var b = {
            webkit: /webkit/.test(n),
            mozilla: (/mozilla/.test(n)) && (!/(compatible|webkit)/.test(n)),
            chrome: /chrome/.test(n),
            msie: ((/msie/.test(n) || /trident/.test(n) || !! window.MSStream) && !/opera/.test(n)),
            firefox: /firefox/.test(n),
            safari: (/safari/.test(n) && !(/chrome/.test(n))),
            opera: /opera/.test(n)
        };
        b.version = (b.safari) ? (n.match(/.+(?:ri)[\/: ]([\d.]+)/) || [])[1] : (n.match(/.+(?:ox|me|ra|ie)[\/: ]([\d.]+)/) || [])[1];
        return b;
    }();

    var trigger = "click";
    var useCapture = (browser.firefox || browser.mozilla || browser.chrome);
    if (browser.chrome) {
    		trigger = "mousedown";
    }

    document.addEventListener ? document.addEventListener(trigger, _$rh.bp, useCapture) : document.attachEvent("on"+trigger, _$rh.bp);

	var g = document.createElement("script");
	g.setAttribute("id","rh_tag_"+tagType+"_PTFFO");
	g.type= "text/javascript";
    g.src= "http://cdn1.rhtag.com/banners/script/bnr-tag_1.0.6.js";

	var scripts = document.getElementsByTagName("script");
	var myScript;
	var found=false;
	for(var i=(scripts.length - 1);i>=0;i--){
		myScript = scripts[i];
		if (myScript.src.indexOf("tid=PTFFO")!=-1) {
			found=true;
			break;
		}
	}
	if (!found) { //fallback
		myScript=scripts[scripts.length - 1];
	}

	if (myScript.parentNode!=document.getElementsByTagName("head")[0]) {
		myScript.parentNode.insertBefore( g, myScript.nextSibling );
	}
	else{
		var bodyOnLoad = function() {
			document.getElementsByTagName('body')[0].appendChild( g);
		};
		
		if(window.addEventListener)
		{
			window.addEventListener("load", bodyOnLoad, false);
		}
		else if (window.attachEvent)
		{
			window.attachEvent("onload", bodyOnLoad);
		} 
	}
	
})();
		
再次感谢您花费宝贵的时间来审视这一点,非常感谢您的帮助。再次感谢。

修改: inject.js文件似乎运行得非常好,因为我在其中尝试了一个“警报”脚本并且它有效。

0 个答案:

没有答案