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, simplify, execute script on "snippets.executeScripts" message Created May 23, 2018, 5:06 a.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
« include.preload.js ('K') | « 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);
Manish Jethani 2018/05/23 05:12:45 Cache executable.
+ 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))
Manish Jethani 2018/05/23 05:12:46 Note: I'm not checking for ELEMHIDE here. I don't
kzar 2018/07/10 14:52:00 So would a #@# exception filter be honoured?
Manish Jethani 2018/07/19 01:00:54 Yes, in the initial version #@# will not work.
+ {
+ return;
+ }
+
+ let hostname = extractHostFromFrame(sender.frame);
+ for (let script of Snippets.getScriptsForDomain(hostname))
+ executeScript(script, sender.page.id, sender.frame.id);
+});
Manish Jethani 2018/05/23 05:12:46 Note: No DevTools logging, I'll get it to eventual
kzar 2018/07/10 14:52:00 Please could you add this as a known limitation to
Manish Jethani 2018/07/19 01:00:53 I do intend to add DevTools logging in the initial
kzar 2018/07/19 10:37:11 Could you mention it in #6782, along with the #@#
+
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,
@@ -204,8 +258,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"})
Manish Jethani 2018/05/23 05:12:46 Note: I deleted the part that loads a remote versi
kzar 2018/07/10 14:52:00 No need to fetch this script, and to add the mappi
Manish Jethani 2018/07/19 01:00:53 We actually need this in text form (not as a JavaS
kzar 2018/07/19 10:37:11 Acknowledged.
+.then(response => response.ok ? response.text() : "")
+.then(text =>
+{
+ snippetsLibrarySource = text;
+});
« include.preload.js ('K') | « include.preload.js ('k') | metadata.chrome » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld