我正在尝试制作广告拦截扩展程序。这是代码:
的manifest.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
。
and 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。每当扩展程序在网站中时,我希望它默认为ON。有谁知道如何实现这一目标。非常感谢任何帮助。
答案 0 :(得分:1)
您可以使用ActiveTab Permission访问当前关注的标签,并最终针对您需要的网站测试标签网址/主机名,然后采取相应措施。 注意:使用“all_urls”是一个坏习惯另一个解决方案正在使用"tabs" 并且一旦列出了确切的网址/网站权限,您的扩展程序将仅在具有给定网址的标签上激活。