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

Unified 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 trace snippet Created July 13, 2018, 2:12 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 | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/content/snippets.js
===================================================================
--- a/lib/content/snippets.js
+++ b/lib/content/snippets.js
@@ -10,18 +10,65 @@
* 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/>.
*/
+/* eslint-env webextensions */
/* eslint no-console: "off" */
"use strict";
+function injectCode(code, dependencies = [])
+{
+ for (let dependency of dependencies)
+ code += dependency;
+
+ let script = document.createElement("script");
+
+ script.type = "application/javascript";
+ script.async = false;
+
+ // Firefox 58 only bypasses site CSPs when assigning to 'src',
+ // while Chrome 67 only bypasses site CSPs when using 'textContent'.
+ if (browser.runtime.getURL("").startsWith("chrome-extension://"))
+ {
+ script.textContent = code;
+ document.documentElement.appendChild(script);
+ }
+ else
+ {
+ let url = URL.createObjectURL(new Blob([code]));
+ script.src = url;
+ document.documentElement.appendChild(script);
+ URL.revokeObjectURL(url);
+ }
+
+ document.documentElement.removeChild(script);
+}
+
+function stringifyFunctionCall(func, ...params)
+{
+ return `"use strict";(${func})(${params.map(JSON.stringify).join(",")});`;
+}
+
+function makeInjector(injectable, ...dependencies)
kzar 2018/07/16 14:13:56 Please could you add a JSDoc comment for this func
Manish Jethani 2018/07/17 10:44:42 Will do.
Manish Jethani 2018/07/17 15:42:36 Done.
+{
+ return (...args) => injectCode(stringifyFunctionCall(injectable, ...args),
kzar 2018/07/16 14:13:56 I'd prefer we did the work of `stringifyFunctionCa
Manish Jethani 2018/07/17 10:44:41 I would really prefer if stringifyFunctionCall is
kzar 2018/07/17 11:11:12 Alright, but what about my comment about making it
Manish Jethani 2018/07/17 15:42:36 That would inject code like: foo(...["x", "y",
kzar 2018/07/17 16:08:21 Acknowledged.
+ dependencies);
+}
+
function log(...args)
{
console.log(...args);
}
exports.log = log;
kzar 2018/07/16 14:13:56 Why not just export `console.log`? I don't think i
Manish Jethani 2018/07/17 10:44:41 Because console.log.toString() is "function log()
kzar 2018/07/17 11:11:12 Good point.
+
+function trace(...args)
Manish Jethani 2018/07/13 14:17:33 I had to do this, both because it helps to have an
kzar 2018/07/16 14:13:57 Please could you add a JSDoc comment explaining th
Manish Jethani 2018/07/17 10:44:41 Will do.
Manish Jethani 2018/07/17 15:42:36 Done.
+{
+ log(...args);
kzar 2018/07/16 14:13:57 Why not call `console.log(...args);` here directly
Manish Jethani 2018/07/17 10:44:42 Because the goal is to demonstrate the use of the
kzar 2018/07/17 11:11:12 Acknowledged.
+}
+
+exports.trace = makeInjector(trace, log);
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld