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

Delta Between Two Patch Sets: lib/content/snippets.js

Issue 29829569: Issue 6538, 6781 - Add code injection wrapper to snippets library (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created July 13, 2018, 2:09 p.m.
Right Patch Set: Remove "safely" from comment, remove unnecessary checks Created July 17, 2018, 5:40 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 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 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 /* eslint-env webextensions */ 18 /* eslint-env webextensions */
19 /* eslint no-console: "off" */ 19 /* eslint no-console: "off" */
20 20
21 "use strict"; 21 "use strict";
22 22
23 /**
24 * Injects JavaScript code into the document using a temporary
25 * <code>script</code> element.
26 *
27 * @param {string} code The code to inject.
28 * @param {Array.<function|string>} [dependencies] A list of dependencies
29 * to inject along with the code. A dependency may be either a function or a
30 * string containing some executable code.
31 */
23 function injectCode(code, dependencies = []) 32 function injectCode(code, dependencies = [])
24 { 33 {
25 for (let dependency of dependencies) 34 for (let dependency of dependencies)
26 code += dependency; 35 code += dependency;
27 36
28 let script = document.createElement("script"); 37 let script = document.createElement("script");
29 38
30 script.type = "application/javascript"; 39 script.type = "application/javascript";
31 script.async = false; 40 script.async = false;
32 41
33 // Firefox 58 only bypasses site CSPs when assigning to 'src', 42 // Firefox 58 only bypasses site CSPs when assigning to 'src',
34 // while Chrome 67 only bypasses site CSPs when using 'textContent'. 43 // while Chrome 67 only bypasses site CSPs when using 'textContent'.
35 if (browser.runtime.getURL("").startsWith("chrome-extension://")) 44 if (browser.runtime.getURL("").startsWith("chrome-extension://"))
36 { 45 {
37 script.textContent = code; 46 script.textContent = code;
38 document.documentElement.appendChild(script); 47 document.documentElement.appendChild(script);
39 } 48 }
40 else 49 else
41 { 50 {
42 let url = URL.createObjectURL(new Blob([code])); 51 let url = URL.createObjectURL(new Blob([code]));
43 script.src = url; 52 script.src = url;
44 document.documentElement.appendChild(script); 53 document.documentElement.appendChild(script);
45 URL.revokeObjectURL(url); 54 URL.revokeObjectURL(url);
46 } 55 }
47 56
48 document.documentElement.removeChild(script); 57 document.documentElement.removeChild(script);
49 } 58 }
50 59
60 /**
61 * Converts a function and an optional list of arguments into a string of code
62 * containing a function call. The function is converted to its string
63 * representation using the <code>Function.prototype.toString</code> method.
64 * Each argument is stringified using <code>JSON.stringify</code>. The
65 * generated code begins with the <code>"use strict"</code> directive.
66 *
67 * @param {function} func The function to convert.
68 * @param {...*} [params] The arguments to convert.
69 *
70 * @returns {string} The generated code containing the function call.
71 */
51 function stringifyFunctionCall(func, ...params) 72 function stringifyFunctionCall(func, ...params)
52 { 73 {
74 // Call JSON.stringify on the arguments to avoid any arbitrary code
75 // execution.
53 return `"use strict";(${func})(${params.map(JSON.stringify).join(",")});`; 76 return `"use strict";(${func})(${params.map(JSON.stringify).join(",")});`;
54 } 77 }
55 78
79 /**
80 * Wraps a function and its dependencies into an injector. The injector, when
81 * called with zero or more arguments, generates code that calls the function,
82 * with the given arguments, if any, and injects the code, along with any
83 * dependencies, into the document using a temporary <code>script</code>
84 * element.
85 *
86 * @param {function} injectable The function to wrap into an injector.
87 * @param {...(function|string)} [dependencies] Any dependencies of the
88 * function. A dependency may be either a function or a string containing
89 * some executable code.
90 *
91 * @returns {function} The generated injector.
92 */
56 function makeInjector(injectable, ...dependencies) 93 function makeInjector(injectable, ...dependencies)
57 { 94 {
58 return (...args) => injectCode(stringifyFunctionCall(injectable, ...args), 95 return (...args) => injectCode(stringifyFunctionCall(injectable, ...args),
59 dependencies); 96 dependencies);
60 } 97 }
61 98
99 /**
100 * Logs its arguments to the console. This may be used for testing and
101 * debugging.
102 *
103 * @param {...*} [args] The arguments to log.
104 */
62 function log(...args) 105 function log(...args)
63 { 106 {
64 console.log(...args); 107 console.log(...args);
65 } 108 }
66 109
67 exports.log = log; 110 exports.log = log;
111
112 /**
113 * Similar to {@link log}, but does the logging in the context of the document
114 * rather than the content script. This may be used for testing and debugging,
115 * especially to verify that the injection of snippets into the document is
116 * working without any errors.
117 *
118 * @param {...*} [args] The arguments to log.
119 */
120 function trace(...args)
121 {
122 // We could simply use console.log here, but the goal is to demonstrate the
123 // usage of snippet dependencies.
124 log(...args);
125 }
126
127 exports.trace = makeInjector(trace, log);
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld