From 63be43a3659f9adce761ef570ea395fa81bc5471 Mon Sep 17 00:00:00 2001 From: gorhill Date: Mon, 21 Aug 2017 12:04:35 -0400 Subject: [PATCH] shield content script against exceptions in injected scriptlets --- src/js/contentscript.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/js/contentscript.js b/src/js/contentscript.js index bbbe37fb1..7e9179613 100644 --- a/src/js/contentscript.js +++ b/src/js/contentscript.js @@ -129,6 +129,20 @@ vAPI.SafeAnimationFrame.prototype.clear = function() { /******************************************************************************/ /******************************************************************************/ +vAPI.injectScriptlet = function(doc, text) { + if ( !doc ) { return; } + try { + var script = doc.createElement('script'); + script.appendChild(doc.createTextNode(text)); + (doc.head || doc.documentElement).appendChild(script); + } catch (ex) { + } +}; + +/******************************************************************************/ +/******************************************************************************/ +/******************************************************************************/ + // The DOM filterer is the heart of uBO's cosmetic filtering. vAPI.domFilterer = (function() { @@ -838,7 +852,6 @@ return domFilterer; // Library of resources is located at: // https://github.com/gorhill/uBlock/blob/master/assets/ublock/resources.txt if ( cfeDetails.scripts ) { - elem = document.createElement('script'); // Have the injected script tag remove itself when execution completes: // to keep DOM as clean as possible. text = cfeDetails.scripts + @@ -850,8 +863,7 @@ return domFilterer; " p.removeChild(c);\n" + " }\n" + "})();"; - elem.appendChild(document.createTextNode(text)); - parent.appendChild(elem); + vAPI.injectScriptlet(document, text); vAPI.injectedScripts = text; } } @@ -1168,12 +1180,7 @@ vAPI.domCollapser = (function() { // and which scripts are selectively looked-up from: // https://github.com/gorhill/uBlock/blob/master/assets/ublock/resources.txt if ( vAPI.injectedScripts ) { - var scriptTag = document.createElement('script'); - scriptTag.appendChild(document.createTextNode(vAPI.injectedScripts)); - var parent = iframe.contentDocument && iframe.contentDocument.head; - if ( parent ) { - parent.appendChild(scriptTag); - } + vAPI.injectScriptlet(iframe.contentDocument, vAPI.injectedScripts); } };