gpt4 book ai didi

javascript - 在某些网站上停止和开始执行扩展

转载 作者:行者123 更新时间:2023-11-30 20:58:33 26 4
gpt4 key购买 nike

我正在尝试制作广告拦截扩展程序。这是代码:

list .json:

    {
"manifest_version":2,
"name":"Ad Killer",
"description":"A Basic program for blocking ads",
"version":"0.7",
"permissions": ["activeTab"],
"background":{
"scripts":[
"background.js"]
},
"content_scripts":[
{
"matches":[
"<all_urls>"
],

"run_at" :"document_end",

"js":[
"jquery-3.2.1.min.js",
"content.js"
]
}
],
"applications":{
"gecko":{
"id":"{99f660be-10c4-4cc8-9205-72dd45f156b9}"
}
},
"browser_action":{
"default_icon":"ad128.png",
"default_title":"Ad Killer"
}
}

Content.js:

/*var elems = document.getElementsByTagName("iframe");*/
var ob2 = $("div[class^='OB_AR_'],div[id*='OB_AR_']");
var ob3 = $("div[id^='outbrain_widget_13'],div[id*='outbrain_']");
var elems2 = $("div[id^='taboola-'],div[id*='taboola']");
var elems3 = $("div[id^='hireme'],div[id*='hireme']");
var elems4 = $("div[id^='yandex'],div[id*='yandex']");
var banner = $("div[id^='rc-'],div[id*='rc-']");
var makeSure = $("div[id^='ad'],div[id*='ad']");
var fkn = $("div[class^='OUTBRAIN'],div[class*='outbrain']");
var yandex1 = $("div[id^='Ya_'],div[id*='ya']");
var ob11 = $("div[id^='adzerk'],div[id*='adzerk']");
var ob4 = $("div[class^='str-adunit str-adunit-mid-article str-card-exp str-collapsed clickout'],div[class*='str-adunit str-adunit-mid-article str-card-exp str-collapsed clickout']");
var trendTags = $("div[class^='trendingTags span-12'],div[class*='trendingTags span-12']");
var bank = $("div[class^='post postFill post-http://www.thejournal.ie/switching-banks-questions-department-of-finance-3355404-Apr2017/'],div[class*='post postFill post-http://www.thejournal.ie/switching-banks-questions-department-of-finance-3355404-Apr2017/']");
var google_ads = $("div[id^='google_ads_iframe_'],div[id*='google_ads_iframe_']");
var tennis1 = $("div[id^='ads'],div[id*='ads']");
var macaron = $("td[class^='marconi'],td[class*='marconi']");
var adidas = $("div[class^='GoogleActiveViewClass'],div[class*='GoogleActiveViewClass']");
var tel = $("div[class^='left-col'],div[class*='left-col']");
var aside = $("section[class^='zn zn-sponsored-outbrain-3 zn-balanced zn--idx-2 zn--ordinary zn-has-two-containers'],section[class^='zn zn-sponsored-outbrain-3 zn-balanced zn--idx-2 zn--ordinary zn-has-two-containers'");
var rh = document.getElementsByTagName("aside");
var adContent = $("div[id^='ad_'], iframe[id*='ad']");
var cloudfront = $("img[src^='//d13dcw'], img[src*='//d13dcw']");
var sidebar = $("div[class^='sidebar'], div[class*='sidebar']");
var jacobs = $("div[id^='jpx-wp-layer-'], div[id*='jpx-wp-layer-]'");
var trc1 = $("div[id^='trc_wrapper'], div[id*='trc_wrapper']");
var mb = $("div[class^='module-body'], div[class*='module-body']");

function hideTRC() {
for (var i = 0, max = trc1.length; i < max; i++) {
trc1[i].hidden = true;
};
}

function hideJacobs() {
for (var i = 0, max = jacobs.length; i < max; i++) {
sidebar[i].hidden = true;
};
}

function hideSide() {
for (var i = 0, max = sidebar.length; i < max; i++) {
sidebar[i].hidden = true;
};
}

function hideIframes() {
for (var i = 0, max = elems.length; i < max; i++) {
elems[i].hidden = true;
};
}

function hideIframes2() {
for (var i = 0, max = elems2.length; i < max; i++) {
elems2[i].hidden = true;
};
}

function hideIframes3() {
for (var i = 0, max = elems3.length; i < max; i++) {
elems3[i].hidden = true;
};
}

function hideIframes4() {
for (var i = 0, max = elems4.length; i < max; i++) {
elems4[i].hidden = true;
};
}

function hideBanner() {
for (var i = 0, max = banner.length; i < max; i++) {
banner[i].hidden = true;
};
}
function makeSure1() {
for (var i = 0, max = makeSure.length; i < max; i++) {
makeSure[i].hidden = true;
};
}

function hideFKN() {
for (var i = 0, max = fkn.length; i < max; i++) {
fkn[i].hidden = true;
};
}

function hideYandex() {
for (var i = 0, max = yandex1.length; i < max; i++) {
yandex1[i].hidden = true;
};
}

function hideOB1() {
for (var i = 0, max = ob11.length; i < max; i++) {
ob11[i].hidden = true;
};
}

function hideOB2() {
for (var i = 0, max = ob2.length; i < max; i++) {
ob2[i].hidden = true;
};

}

function hideOB3() {
for (var i = 0, max = ob3.length; i < max; i++) {
ob3[i].hidden = true;
};

}

function hideOB4() {
for (var i = 0, max = ob4.length; i < max; i++) {
ob4[i].hidden = true;
};

}

function hideTTags() {
for (var i = 0, max = trendTags.length; i < max; i++) {
trendTags[i].hidden = true;
};

}

function hideBank() {
for (var i = 0, max = bank.length; i < max; i++) {
bank[i].hidden = true;
};

}

function hideGAds() {
for (var i = 0, max = google_ads.length; i < max; i++) {
google_ads[i].hidden = true;
};

}

function hideTennis() {
for (var i = 0, max = tennis1.length; i < max; i++) {
tennis1[i].hidden = true;
};

}

function hideMacaron() {
for (var i = 0, max = macaron.length; i < max; i++) {
macaron[i].hidden = true;
};

}

function hideAdidas() {
for (var i = 0, max = adidas.length; i < max; i++) {
adidas[i].hidden = true;
};

}

function hideTel() {
for (var i = 0, max = tel.length; i < max; i++) {
tel[i].hidden = true;
};

}

function hideRH() {
for (var i = 0, max = rh.length; i < max; i++) {
rh[i].hidden = true;
};

}

function hideAside() {
for (var i = 0, max = aside.length; i < max; i++) {
aside[i].hidden = true;
};

}

function hideG2() {
for (var i = 0, max = google_ads2.length; i < max; i++) {
google_ads2[i].hidden = true;
};

}

function hideAdContent() {
for (var i = 0, max = adContent.length; i < max; i++) {
adContent[i].hidden = true;
};

}

function hideCloud() {
for (var i = 0, max = cloudfront.length; i < max; i++) {
cloudfront[i].hidden = true;
};

}

function hideMB() {
for (var i = 0, max = mb.length; i < max; i++) {
mb[i].hidden = true;
};

}

$(document).ready(function() {
//hideIframes();
hideOB2();
//hideIframes2();
//hideIframes3();
hideIframes4();
hideBanner();
makeSure1();
hideFKN();
hideYandex();
hideOB1();
hideOB4();
hideTTags();
hideBank();
hideGAds();
hideTennis();
hideMacaron();
hideAdidas();
hideTel();
hideRH();
hideAside();
hideAdContent();
hideCloud();
hideSide();
hideJacobs();
hideTRC();
hideMB();
});

window.onload = function() {
//hideIframes();
hideOB2();
//hideIframes2();
//hideIframes3();
hideIframes4();
hideBanner();
makeSure1();
hideFKN();
hideYandex();
hideOB1();
hideOB4();
hideTTags();
hideBank();
hideGAds();
hideTennis();
hideMacaron();
hideAdidas();
hideTel();
hideRH();
hideAside();
hideAdContent();
hideCloud();
hideSide();
hideJacobs();
hideTRC();
hideMB();
};

$(window).on('load', function() {
//hideIframes();
hideOB2();
//hideIframes2();
//hideIframes3();
hideIframes4();
hideIframes();
hideBanner();
makeSure1();
hideFKN();
hideYandex();
hideOB1();
hideOB4();
hideTTags();
hideBank();
hideGAds();
hideTennis();
hideMacaron();
hideAdidas();
hideTel();
hideRH();
hideAside();
hideAdContent();
hideCloud();
hideSide();
hideJacobs();
hideTRC();
hideMB();

});
/*
do {
setTimeout(hideIframes, 3000);
}
while (elems > 0);

if (elems > 0) {
setTimeout(hideIframes, 3000);
}

do {
setTimeout(hideIframes3, 3000);
hideIframes3();
}
while (elems3 > 0);
*/
do {
hideOB1();
}

while (ob11 > 0);

hideRH();

restore.js 实际上是 content.js 除了所有隐藏属性都是 false

和 background.js:

var enable = false;
enable = enable ? false : true;
chrome.browserAction.onClicked.addListener(function (tab) {
enable = enable ? false : true;
if(enable){
chrome.browserAction.setBadgeText({ text: 'ON' });
//location.reload();
chrome.tabs.executeScript(null, { file: 'content.js' });
} else {
enable = false;
chrome.browserAction.setBadgeText({ text: 'OFF' });
//chrome.tabs.executeScript(tab.id, {code:"location.reload(false);"});
chrome.tabs.executeScript(null, { file: 'restore.js' });
}
});

这一切都编译得很好,但我想做一件事。每次用户访问新标签页(chrome://newtab)时,我都希望脚本停止运行并将切换状态更改为 OFF。每当扩展程序在网站上时,我都希望它默认打开。有谁知道如何实现这一目标。非常感谢任何帮助。

最佳答案

您可以使用 ActiveTab Permission访问当前聚焦的选项卡并最终针对您需要的网站测试选项卡 Url/主机名,然后采取相应的措施。注意:使用“all_urls”是一个坏习惯另一个解决方案是使用"tabs"一旦您列出了确切的 url/网站权限,您的扩展程序将仅在具有给定 url 的选项卡上激活。

关于javascript - 在某些网站上停止和开始执行扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47375899/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com