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: Use JSON.stringify Created March 31, 2018, 8:06 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
« 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 {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"})
33 .then(response => response.text())
34 .then(text =>
35 {
36 library = text;
37 });
38
39 function injectCode(snippet, tabId, frameId)
40 {
41 if (!library)
42 return;
43
44 browser.tabs.executeScript(tabId, {
45 code: `((${library})[${JSON.stringify(snippet)}] || (() => {}))()`,
46 frameId,
47 matchAboutBlank: true,
48 runAt: "document_start"
49 });
50 }
51
52 browser.webNavigation.onCommitted.addListener(details =>
53 {
54 // There's a bug in Chrome that causes webNavigation.onCommitted to get
55 // dispatched twice if there's a URL filter present, therefore we must listen
56 // for all URLs and do an explicit check here.
57 if (!/^https?:\/\//.test(details.url))
58 return;
59
60 let url = new URL(details.url);
61 let urlString = stringifyURL(url);
62 let parentFrame = ext.getFrame(details.tabId, details.parentFrameId);
63 let hostname = extractHostFromFrame(parentFrame) || getDecodedHostname(url);
64 let thirdParty = isThirdParty(url, hostname);
65
66 let filter = defaultMatcher.matchesAny(urlString, typeMap.SNIPPET, hostname,
67 thirdParty, null, true);
68 if (!filter)
69 return;
70
71 let page = new ext.Page({id: details.tabId, url: details.url});
72 let frame = ext.getFrame(details.tabId, details.frameId);
73
74 if (checkWhitelisted(page, frame))
75 return;
76
77 devtools.logRequest(page, urlString, "SNIPPET", hostname, thirdParty, null,
78 true, filter);
79 FilterNotifier.emit("filter.hitCount", filter, 0, 0, page);
80
81 if (filter instanceof WhitelistFilter)
82 return;
83
84 for (let snippet of filter.snippets)
85 injectCode(snippet, details.tabId, details.frameId);
86 });
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