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 snippet library like a module Created March 31, 2018, 9:28 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
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 {extractHostFromFrame, getDecodedHostname,
23 isThirdParty, stringifyURL} = require("url");
24 const {checkWhitelisted} = require("whitelisting");
25 const {FilterNotifier} = require("filterNotifier");
26 const devtools = require("devtools");
27
28 const {typeMap} = RegExpFilter;
29
30 let library = null;
31 let executableCode = new Map();
Manish Jethani 2018/03/31 09:36:26 This cache is not strictly necessary but it would
32
33 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"})
34 .then(response => response.text())
35 .then(text =>
36 {
37 library = text;
38 });
39
40 function getExecutableCode(snippet)
41 {
42 let code = executableCode.get(snippet);
43 if (code)
44 return code;
45
46 code = `
47 "use strict";
48 {
49 let imports = Object.create(null);
50 new Function("exports", ${JSON.stringify(library)})(imports);
Manish Jethani 2018/03/31 09:36:26 So we're loading the snippet library as a function
51 let key = ${JSON.stringify(snippet)};
52 if (Object.prototype.hasOwnProperty.call(imports, key))
Manish Jethani 2018/03/31 09:36:26 Just to be extra safe it's better to check that th
53 {
54 let value = imports[key];
55 if (typeof value == "function")
56 value();
57 }
58 }
59 `;
60
61 executableCode.set(snippet, code);
62 return code;
63 }
64
65 function injectCode(snippet, tabId, frameId)
66 {
67 if (!library)
68 return;
69
70 browser.tabs.executeScript(tabId, {
71 code: getExecutableCode(snippet),
72 frameId,
73 matchAboutBlank: true,
74 runAt: "document_start"
75 });
76 }
77
78 browser.webNavigation.onCommitted.addListener(details =>
79 {
80 // There's a bug in Chrome that causes webNavigation.onCommitted to get
81 // dispatched twice if there's a URL filter present, therefore we must listen
82 // for all URLs and do an explicit check here.
83 if (!/^https?:\/\//.test(details.url))
84 return;
85
86 let url = new URL(details.url);
87 let urlString = stringifyURL(url);
88 let parentFrame = ext.getFrame(details.tabId, details.parentFrameId);
89 let hostname = extractHostFromFrame(parentFrame) || getDecodedHostname(url);
90 let thirdParty = isThirdParty(url, hostname);
91
92 let filter = defaultMatcher.matchesAny(urlString, typeMap.SNIPPET, hostname,
93 thirdParty, null, true);
94 if (!filter)
95 return;
96
97 let page = new ext.Page({id: details.tabId, url: details.url});
98 let frame = ext.getFrame(details.tabId, details.frameId);
99
100 if (checkWhitelisted(page, frame))
101 return;
102
103 devtools.logRequest(page, urlString, "SNIPPET", hostname, thirdParty, null,
104 true, filter);
105 FilterNotifier.emit("filter.hitCount", filter, 0, 0, page);
106
107 if (filter instanceof WhitelistFilter)
108 return;
109
110 for (let snippet of filter.snippets)
111 injectCode(snippet, details.tabId, details.frameId);
112 });
OLDNEW
« no previous file with comments | « lib/requestBlocker.js ('k') | metadata.chrome » ('j') | snippets.js » ('J')

Powered by Google App Engine
This is Rietveld