我正在制作Chrome扩展程序,用于从网页中删除广告。这是我的代码:
manifest.json
:
{
"manifest_version":2,
"name":"Ad Killer",
"description":"A Basic program for blocking ads",
"version":"0.5",
"permissions": ["activeTab"],
"background":{
"scripts":[
"background.js"]
},
"content_scripts":[
{
"matches":[
"<all_urls>"
],
"js":[
"jquery-3.2.1.min.js"
]
}
],
"applications":{
"gecko":{
"id":"{99f660be-10c4-4cc8-9205-72dd45f156b9}"
}
},
"browser_action":{
"default_icon":"ad128.png",
"default_title":"Ad Killer"
}
}
我的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);"});
}
});
最后,我的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'");
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;
};
}
$(document).ready(function() {
hideIframes();
hideOB2();
hideIframes2();
hideIframes3();
hideIframes4();
hideBanner();
makeSure1();
hideFKN();
hideYandex();
hideOB1();
hideOB4();
hideTTags();
hideBank();
hideGAds();
hideTennis();
hideMacaron();
hideAdidas();
hideTel();
hideRH();
hideAside();
hideAdContent();
});
window.onload = function() {
hideIframes();
hideOB2();
hideIframes2();
hideIframes3();
hideIframes4();
hideBanner();
makeSure1();
hideFKN();
hideYandex();
hideOB1();
hideOB4();
hideTTags();
hideBank();
hideGAds();
hideTennis();
hideMacaron();
hideAdidas();
hideTel();
hideRH();
hideAside();
hideAdContent();
};
$(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();
});
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();
所以这是我的问题:基本上,当用户点击图标时,它会显示一个徽章,并删除用户访问过的每个页面的广告,直到用户再次点击该图标把它关掉。问题是,如果我打开它并转到另一个页面,广告就会显示出来。是否有一种方法可以在用户单击图标将其打开时将脚本注入每个页面? 任何帮助表示赞赏。