Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/snippets.js

Issue 29737561: Issue 6539, 6782 - Implement support for snippets (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Load public key off disk Created March 31, 2018, 1:49 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/requestBlocker.js ('k') | metadata.chrome » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 "use strict";
19
20 const {defaultMatcher} = require("matcher");
21 const {RegExpFilter, WhitelistFilter} = require("filterClasses");
22 const {verifySignature} = require("rsa");
23 const {extractHostFromFrame, getDecodedHostname,
24 isThirdParty, stringifyURL} = require("url");
25 const {checkWhitelisted} = require("whitelisting");
26 const {FilterNotifier} = require("filterNotifier");
27 const devtools = require("devtools");
28 const info = require("info");
29
30 const {typeMap} = RegExpFilter;
31
32 const remoteURL = "https://easylist-downloads.adblockplus.org/snippets.js";
33
34 let libraries = {local: "", remote: ""};
35 let executableCode = new Map();
36
37 function fetchText(url)
38 {
39 return fetch(url, {cache: "no-cache"}).then(
40 response => response.ok ? response.text() : ""
41 );
42 }
43
44 function updateLibrary(name, text)
45 {
46 libraries[name] = text;
47
48 executableCode.clear();
49 }
50
51 function checkLibrarySignature(url, text)
52 {
53 return fetchText(`${url}.sig`).then(
54 signature => fetchText(browser.extension.getURL("/key")).then(
55 key => verifySignature(key.replace(/=/g, ""), signature, text)
56 )
57 );
58 }
59
60 function loadLibrary(name, url, {verify = true} = {})
61 {
62 fetchText(url).then(text =>
63 {
64 if (text != libraries[name])
65 {
66 let check = verify ? checkLibrarySignature(url, text) :
67 Promise.resolve(true);
68 check.then(ok =>
69 {
70 if (ok)
71 updateLibrary(name, text);
72 });
73 }
74 });
75 }
76
77 function loadLocalLibrary()
78 {
79 loadLibrary("local", browser.extension.getURL("/snippets.js"),
80 {verify: false});
81 }
82
83 function loadRemoteLibrary()
84 {
85 loadLibrary("remote", remoteURL);
86 }
87
88 function getExecutableCode(snippet)
89 {
90 let code = executableCode.get(snippet);
91 if (code)
92 return code;
93
94 code = `
95 "use strict";
96 {
97 let localImports = Object.create(null);
98 let remoteImports = Object.create(null);
99 new Function("exports", ${JSON.stringify(libraries.local)})(
100 localImports
101 );
102 new Function("exports", ${JSON.stringify(libraries.remote)})(
103 remoteImports
104 );
105 let imports = Object.assign(Object.create(null),
106 localImports,
107 remoteImports);
108 let key = ${JSON.stringify(snippet)};
109 if (Object.prototype.hasOwnProperty.call(imports, key))
110 {
111 let value = imports[key];
112 if (typeof value == "function")
113 value();
114 }
115 }
116 `;
117
118 executableCode.set(snippet, code);
119 return code;
120 }
121
122 function injectCode(snippet, tabId, frameId)
123 {
124 browser.tabs.executeScript(tabId, {
125 code: getExecutableCode(snippet),
126 frameId,
127 matchAboutBlank: true,
128 runAt: "document_start"
129 });
130 }
131
132 loadLocalLibrary();
133
134 // Only Chrome supports dynamic loading of JS.
135 if (info.platform == "chromium")
136 {
137 loadRemoteLibrary();
138
139 // Download every 24 hours.
140 setInterval(loadRemoteLibrary, 24 * 60 * 60 * 1000);
141 }
142
143 browser.webNavigation.onCommitted.addListener(details =>
144 {
145 // There's a bug in Chrome that causes webNavigation.onCommitted to get
146 // dispatched twice if there's a URL filter present, therefore we must listen
147 // for all URLs and do an explicit check here.
148 if (!/^https?:\/\//.test(details.url))
149 return;
150
151 let url = new URL(details.url);
152 let urlString = stringifyURL(url);
153 let parentFrame = ext.getFrame(details.tabId, details.parentFrameId);
154 let hostname = extractHostFromFrame(parentFrame) || getDecodedHostname(url);
155 let thirdParty = isThirdParty(url, hostname);
156
157 let filter = defaultMatcher.matchesAny(urlString, typeMap.SNIPPET, hostname,
158 thirdParty, null, true);
159 if (!filter)
160 return;
161
162 let page = new ext.Page({id: details.tabId, url: details.url});
163 let frame = ext.getFrame(details.tabId, details.frameId);
164
165 if (checkWhitelisted(page, frame))
166 return;
167
168 devtools.logRequest(page, urlString, "SNIPPET", hostname, thirdParty, null,
169 true, filter);
170 FilterNotifier.emit("filter.hitCount", filter, 0, 0, page);
171
172 if (filter instanceof WhitelistFilter)
173 return;
174
175 for (let snippet of filter.snippets)
176 injectCode(snippet, details.tabId, details.frameId);
177 });
OLDNEW
« no previous file with comments | « lib/requestBlocker.js ('k') | metadata.chrome » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld