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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/snippets.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/lib/snippets.js
@@ -0,0 +1,88 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-present eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+"use strict";
+
+const {defaultMatcher} = require("matcher");
+const {RegExpFilter, WhitelistFilter} = require("filterClasses");
+const {extractHostFromFrame, getDecodedHostname,
+ isThirdParty, stringifyURL} = require("url");
+const {checkWhitelisted} = require("whitelisting");
+const {FilterNotifier} = require("filterNotifier");
+const devtools = require("devtools");
+
+const {typeMap} = RegExpFilter;
+
+let library = null;
+
+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
+.then(response => response.text())
+.then(text =>
+{
+ library = text;
Manish Jethani 2018/03/30 20:33:15 The "library" is the entire text of the file. See
+});
+
+function injectCode(snippet, tabId, frameId)
+{
+ if (!library)
+ return;
+
+ 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
+
+ browser.tabs.executeScript(tabId, {
+ code: `((${library})["${name}"] || (() => {}))()`,
+ frameId,
+ matchAboutBlank: true,
+ runAt: "document_start"
+ });
+}
+
+browser.webNavigation.onCommitted.addListener(details =>
+{
+ // There's a bug in Chrome that causes webNavigation.onCommitted to get
+ // dispatched twice if there's a URL filter present, therefore we must listen
+ // for all URLs and do an explicit check here.
+ 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
+ return;
+
+ let url = new URL(details.url);
+ let urlString = stringifyURL(url);
+ let parentFrame = ext.getFrame(details.tabId, details.parentFrameId);
+ let hostname = extractHostFromFrame(parentFrame) || getDecodedHostname(url);
+ let thirdParty = isThirdParty(url, hostname);
+
+ let filter = defaultMatcher.matchesAny(urlString, typeMap.SNIPPET, hostname,
+ thirdParty, null, true);
Manish Jethani 2018/03/30 20:39:25 Note that here we're explicitly asking for domain-
+ if (!filter)
+ return;
+
+ let page = new ext.Page({id: details.tabId, url: details.url});
+ let frame = ext.getFrame(details.tabId, details.frameId);
+
+ if (checkWhitelisted(page, frame))
+ return;
+
+ devtools.logRequest(page, urlString, "SNIPPET", hostname, thirdParty, null,
+ true, filter);
+ FilterNotifier.emit("filter.hitCount", filter, 0, 0, page);
+
+ if (filter instanceof WhitelistFilter)
+ return;
+
+ for (let snippet of filter.snippets)
+ injectCode(snippet, details.tabId, details.frameId);
+});

Powered by Google App Engine
This is Rietveld