-
Notifications
You must be signed in to change notification settings - Fork 0
/
New Text Document.txt
97 lines (93 loc) · 2.43 KB
/
New Text Document.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// manifest.json
{
"name": "AdBlock",
"version": "1.0",
"description": "A simple AdBlockPro extension for Chrome by IMON.",
"manifest_version": 2,
"content_scripts": [
{
"matches": ["*://*/*"],
"js": ["contentscript.js"]
}
],
"permissions": ["tabs", "contextMenus"]
}
// contentscript.js
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (changeInfo.status == "complete") {
// Block all ads.
chrome.webRequest.onHeadersReceived.addListener(
function(details) {
if (details.url.match(/\b(ad|ads)\b/)) {
details.responseHeaders["Content-Length"] = "0";
}
},
{
urls: ["*://*/*"],
types: ["main_frame"],
frameId: details.frameId
}
);
// Block analytics features.
chrome.webRequest.onHeadersReceived.addListener(
function(details) {
if (details.url.match(/\b(analytics|ga)\b/)) {
details.responseHeaders["Content-Length"] = "0";
}
},
{
urls: ["*://*/*"],
types: ["main_frame"],
frameId: details.frameId
}
);
}
});
// popup.html
<!DOCTYPE html>
<html>
<head>
<title>AdBlock</title>
</head>
<body>
<h1>AdBlock</h1>
<p>This extension blocks all ads and analytics features on websites.</p>
<p>You can turn off blocking for specific websites by clicking the "Allow Ads" button.</p>
<button id="allowAds">Allow Ads</button>
</body>
</html>
// popup.js
chrome.runtime.onInstalled.addListener(function() {
chrome.tabs.create({
url: "popup.html"
});
});
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request == "allowAds") {
chrome.webRequest.onHeadersReceived.removeListener(
function(details) {
if (details.url.match(/\b(ad|ads)\b/)) {
details.responseHeaders["Content-Length"] = "0";
}
},
{
urls: ["*://*/*"],
types: ["main_frame"],
frameId: details.frameId
}
);
chrome.webRequest.onHeadersReceived.removeListener(
function(details) {
if (details.url.match(/\b(analytics|ga)\b/)) {
details.responseHeaders["Content-Length"] = "0";
}
},
{
urls: ["*://*/*"],
types: ["main_frame"],
frameId: details.frameId
}
);
sendResponse("success");
}
});