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: Created March 30, 2018, 8:25 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
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
32 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"})
Manish Jethani 2018/03/30 20:33:15 Using the fetch API to load the snippets library.
Manish Jethani 2018/03/30 20:39:25 So this is literally just a file that is bundled w
33 .then(response => response.text())
34 .then(text =>
35 {
36 library = text;
Manish Jethani 2018/03/30 20:33:15 The "library" is the entire text of the file. See
37 });
38
39 function injectCode(snippet, tabId, frameId)
40 {
41 if (!library)
42 return;
43
44 let name = snippet.replace("\"", "\\\"");
Manish Jethani 2018/03/30 20:33:15 The name could contains quotes so we need to escap
Manish Jethani 2018/03/31 08:09:40 It turns out the correct way to quote a string is
45
46 browser.tabs.executeScript(tabId, {
47 code: `((${library})["${name}"] || (() => {}))()`,
48 frameId,
49 matchAboutBlank: true,
50 runAt: "document_start"
51 });
52 }
53
54 browser.webNavigation.onCommitted.addListener(details =>
55 {
56 // There's a bug in Chrome that causes webNavigation.onCommitted to get
57 // dispatched twice if there's a URL filter present, therefore we must listen
58 // for all URLs and do an explicit check here.
59 if (!/^https?:\/\//.test(details.url))
Manish Jethani 2018/03/30 20:33:15 I'll try to make a standalone test case for this a
60 return;
61
62 let url = new URL(details.url);
63 let urlString = stringifyURL(url);
64 let parentFrame = ext.getFrame(details.tabId, details.parentFrameId);
65 let hostname = extractHostFromFrame(parentFrame) || getDecodedHostname(url);
66 let thirdParty = isThirdParty(url, hostname);
67
68 let filter = defaultMatcher.matchesAny(urlString, typeMap.SNIPPET, hostname,
69 thirdParty, null, true);
Manish Jethani 2018/03/30 20:39:25 Note that here we're explicitly asking for domain-
70 if (!filter)
71 return;
72
73 let page = new ext.Page({id: details.tabId, url: details.url});
74 let frame = ext.getFrame(details.tabId, details.frameId);
75
76 if (checkWhitelisted(page, frame))
77 return;
78
79 devtools.logRequest(page, urlString, "SNIPPET", hostname, thirdParty, null,
80 true, filter);
81 FilterNotifier.emit("filter.hitCount", filter, 0, 0, page);
82
83 if (filter instanceof WhitelistFilter)
84 return;
85
86 for (let snippet of filter.snippets)
87 injectCode(snippet, details.tabId, details.frameId);
88 });
OLDNEW

Powered by Google App Engine
This is Rietveld