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

Side by Side Diff: lib/content/snippets.js

Issue 29829569: Issue 6538, 6781 - Add code injection wrapper to snippets library (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Add JSDoc comments for log and trace snippets Created July 17, 2018, 3:39 p.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 | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 no-console: "off" */ 19 /* eslint no-console: "off" */
19 20
20 "use strict"; 21 "use strict";
21 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 */
32 function injectCode(code, dependencies = [])
33 {
34 for (let dependency of dependencies)
35 code += dependency;
36
37 let script = document.createElement("script");
38
39 script.type = "application/javascript";
40 script.async = false;
41
42 // Firefox 58 only bypasses site CSPs when assigning to 'src',
43 // while Chrome 67 only bypasses site CSPs when using 'textContent'.
44 if (browser.runtime.getURL("").startsWith("chrome-extension://"))
45 {
46 script.textContent = code;
47 document.documentElement.appendChild(script);
48 }
49 else
50 {
51 let url = URL.createObjectURL(new Blob([code]));
52 script.src = url;
53 document.documentElement.appendChild(script);
54 URL.revokeObjectURL(url);
55 }
56
57 document.documentElement.removeChild(script);
58 }
59
60 /**
61 * Safely converts a function and an optional list of arguments into a string
kzar 2018/07/17 16:08:22 ("Safely" seems a bit optimistic, when we're talki
Manish Jethani 2018/07/17 16:39:33 I really want it to be safe, but thanks to your co
kzar 2018/07/17 16:59:19 I guess I don't understand if they are able to mes
Manish Jethani 2018/07/17 17:27:02 You're right, at this point I am merely being over
kzar 2018/07/18 07:40:08 Well, I'm all for being cautious, but if we leave
62 * of code 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 */
72 function stringifyFunctionCall(func, ...params)
73 {
74 return `"use strict";(${func})(${params.map(JSON.stringify).join(",")});`;
75 }
76
77 /**
78 * Wraps a function and its dependencies into an injector. The injector, when
79 * called with zero or more arguments, generates code that calls the function,
kzar 2018/07/17 16:08:22 Nit: "with zero or more arguments" can be removed,
Manish Jethani 2018/07/17 16:39:33 Acknowledged. I'll leave it as it is though, if y
kzar 2018/07/17 16:59:19 OK.
80 * with the given arguments, if any, and injects the code, along with any
81 * dependencies, into the document using a temporary <code>script</code>
82 * element.
83 *
84 * @param {function} injectable The function to wrap into an injector.
85 * @param {...(function|string)} [dependencies] Any dependencies of the
86 * function. A dependency may be either a function or a string containing
87 * some executable code.
88 *
89 * @returns {function} The generated injector.
90 */
91 function makeInjector(injectable, ...dependencies)
92 {
93 return (...args) => injectCode(stringifyFunctionCall(injectable, ...args),
94 dependencies);
95 }
96
97 /**
98 * Logs its arguments to the console. This may be used for testing and
99 * debugging.
100 *
101 * @param {...*} [args] The arguments to log.
102 */
22 function log(...args) 103 function log(...args)
23 { 104 {
24 console.log(...args); 105 console.log(...args);
25 } 106 }
26 107
27 exports.log = log; 108 exports.log = log;
109
110 /**
111 * Similar to {@link log}, but does the logging in the context of the document
112 * rather than the content script. This may be used for testing and debugging,
113 * especially to verify that the injection of snippets into the document is
114 * working without any errors.
115 *
116 * @param {...*} [args] The arguments to log.
117 */
118 function trace(...args)
119 {
120 // We could simply use console.log here, but the goal is to demonstrate the
121 // usage of snippet dependencies.
122 log(...args);
123 }
124
125 exports.trace = makeInjector(trace, log);
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld