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

Unified Diff: lib/scriptInjection.js

Issue 29737561: Issue 6539, 6782 - Implement support for snippets (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Rebase, use snippets library from core Created July 13, 2018, 2:59 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
« no previous file with comments | « include.preload.js ('k') | metadata.chrome » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/scriptInjection.js
===================================================================
rename from lib/cssInjection.js
rename to lib/scriptInjection.js
--- a/lib/cssInjection.js
+++ b/lib/scriptInjection.js
@@ -10,39 +10,43 @@
* 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/>.
*/
-/** @module cssInjection */
+/** @module scriptInjection */
"use strict";
const {RegExpFilter} = require("../adblockpluscore/lib/filterClasses");
const {ElemHide} = require("../adblockpluscore/lib/elemHide");
const {ElemHideEmulation} = require("../adblockpluscore/lib/elemHideEmulation");
+const {Snippets, compileScript} = require("../adblockpluscore/lib/snippets");
const {checkWhitelisted} = require("./whitelisting");
const {extractHostFromFrame} = require("./url");
const {port} = require("./messaging");
const {HitLogger} = require("./hitLogger");
const info = require("info");
// Chromium's support for tabs.removeCSS is still a work in progress and the
// API is likely to be different from Firefox's; for now we just don't use it
// at all, even if it's available.
// See https://crbug.com/608854
const styleSheetRemovalSupported = info.platform == "gecko";
const selectorGroupSize = 1024;
let userStyleSheetsSupported = true;
+let snippetsLibrarySource = "";
+let executableCode = new Map();
+
function* splitSelectors(selectors)
{
// Chromium's Blink engine supports only up to 8,192 simple selectors, and
// even fewer compound selectors, in a rule. The exact number of selectors
// that would work depends on their sizes (e.g. "#foo .bar" has a size of 2).
// Since we don't know the sizes of the selectors here, we simply split them
// into groups of 1,024, based on the reasonable assumption that the average
// selector won't have a size greater than 8. The alternative would be to
@@ -155,16 +159,66 @@
// style sheet now.
if (oldStyleSheet && oldStyleSheet != styleSheet)
removeStyleSheet(tabId, frameId, oldStyleSheet);
frame.injectedStyleSheets.set(groupName, styleSheet);
return true;
}
+function getExecutableCode(script)
+{
+ let code = executableCode.get(script);
+ if (code)
+ return code;
+
+ code = compileScript(script, [snippetsLibrarySource]);
+
+ executableCode.set(script, code);
+ return code;
+}
+
+function executeScript(script, tabId, frameId)
+{
+ try
+ {
+ browser.tabs.executeScript(tabId, {
+ code: getExecutableCode(script),
+ frameId,
+ matchAboutBlank: true,
+ runAt: "document_start"
+ })
+ .catch(error =>
+ {
+ // Sometimes a frame is added and removed very quickly, in such cases we
+ // simply ignore the error.
+ if (error.message == "The frame was removed.")
+ return;
+
+ throw error;
+ });
+ }
+ catch (error)
+ {
+ }
+}
+
+port.on("snippets.executeScripts", (message, sender) =>
+{
+ if (checkWhitelisted(sender.page, sender.frame, null,
+ RegExpFilter.typeMap.DOCUMENT))
+ {
+ return;
+ }
+
+ let hostname = extractHostFromFrame(sender.frame);
+ for (let script of Snippets.getScriptsForDomain(hostname))
+ executeScript(script, sender.page.id, sender.frame.id);
+});
+
port.on("elemhide.getSelectors", (message, sender) =>
{
let selectors = [];
let emulatedPatterns = [];
let trace = HitLogger.hasListener(sender.page.id);
let inline = !userStyleSheetsSupported;
if (!checkWhitelisted(sender.page, sender.frame, null,
@@ -201,8 +255,15 @@
return response;
});
port.on("elemhide.injectSelectors", (message, sender) =>
{
updateFrameStyles(sender.page.id, sender.frame.id, message.selectors,
message.groupName, message.appendOnly);
});
+
+fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"})
+.then(response => response.ok ? response.text() : "")
+.then(text =>
+{
+ snippetsLibrarySource = text;
+});
« no previous file with comments | « include.preload.js ('k') | metadata.chrome » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld