diff --git a/app/adapters/web-extension.js b/app/adapters/web-extension.js index e7db10a6b1..516b4b53e2 100644 --- a/app/adapters/web-extension.js +++ b/app/adapters/web-extension.js @@ -53,8 +53,12 @@ export default BasicAdapter.extend({ }, _injectDebugger() { - loadEmberDebug().then((emberDebug) => { - chrome.devtools.inspectedWindow.eval(emberDebug); + loadEmberDebug().then(emberDebug => { + chrome.devtools.inspectedWindow.eval(emberDebug, (success, error) => { + if (success === undefined && error) { + throw error; + } + }); this.onResourceAdded(); }); }, @@ -96,7 +100,10 @@ export default BasicAdapter.extend({ * @param {String} goToVersion */ onVersionMismatch(goToVersion) { - window.location.href = `../panes-${goToVersion.replace(/\./g, '-')}/index.html`; + window.location.href = `../panes-${goToVersion.replace( + /\./g, + '-' + )}/index.html`; }, /** @@ -104,16 +111,15 @@ export default BasicAdapter.extend({ scripts as soon as possible into the new page. */ reloadTab() { - loadEmberDebug().then((emberDebug) => { + loadEmberDebug().then(emberDebug => { chrome.devtools.inspectedWindow.reload({ injectedScript: emberDebug }); }); - }, canOpenResource: false, sendIframes(urls) { - loadEmberDebug().then((emberDebug) => { + loadEmberDebug().then(emberDebug => { urls.forEach(url => { chrome.devtools.inspectedWindow.eval(emberDebug, { frameURL: url }); }); @@ -124,7 +130,7 @@ export default BasicAdapter.extend({ function loadEmberDebug() { let minimumVersion = config.emberVersionsSupported[0].replace(/\./g, '-'); let xhr; - return new Promise((resolve) => { + return new Promise(resolve => { if (!emberDebug) { xhr = new XMLHttpRequest(); xhr.open("GET", chrome.runtime.getURL(`/panes-${minimumVersion}/ember_debug.js`)); diff --git a/ember_debug/vendor/startup-wrapper.js b/ember_debug/vendor/startup-wrapper.js index 0583377260..f294f6a827 100644 --- a/ember_debug/vendor/startup-wrapper.js +++ b/ember_debug/vendor/startup-wrapper.js @@ -24,9 +24,6 @@ var EMBER_VERSIONS_SUPPORTED = {{EMBER_VERSIONS_SUPPORTED}}; // @formatter:on (function(adapter) { - var onReady = requireModule('ember-debug/utils/on-ready').onReady; - var compareVersion = requireModule('ember-debug/utils/version').compareVersion; - onEmberReady(function() { // global to prevent injection if (window.NO_EMBER_DEBUG) { @@ -219,6 +216,12 @@ var EMBER_VERSIONS_SUPPORTED = {{EMBER_VERSIONS_SUPPORTED}}; }); } + let channel = new MessageChannel(); + let port = channel.port1; + window.postMessage('debugger-client', '*', [channel.port2]); + + let registeredMiss = false; + /** * This function is called if the app's Ember version * is not supported by this version of the inspector. @@ -227,16 +230,24 @@ var EMBER_VERSIONS_SUPPORTED = {{EMBER_VERSIONS_SUPPORTED}}; * to an inspector version that supports this Ember version. */ function sendVersionMiss() { - var adapter = requireModule('ember-debug/adapters/' + currentAdapter)['default'].create(); - adapter.onMessageReceived(function(message) { + if (registeredMiss) { + return; + } + + registeredMiss = true; + + port.addEventListener('message', message => { if (message.type === 'check-version') { sendVersionMismatch(); } }); + sendVersionMismatch(); + port.start(); + function sendVersionMismatch() { - adapter.sendMessage({ + port.postMessage({ name: 'version-mismatch', version: Ember.VERSION, from: 'inspectedWindow' @@ -277,4 +288,74 @@ var EMBER_VERSIONS_SUPPORTED = {{EMBER_VERSIONS_SUPPORTED}}; return !toVersion || compareVersion(version, toVersion) === -1; } + function onReady(callback) { + if (document.readyState === 'complete' || document.readyState === 'interactive') { + setTimeout(completed); + } else { + document.addEventListener("DOMContentLoaded", completed, false); + // For some reason DOMContentLoaded doesn't always work + window.addEventListener("load", completed, false); + } + + function completed() { + document.removeEventListener("DOMContentLoaded", completed, false); + window.removeEventListener("load", completed, false); + callback(); + } + } + + /** + * Compares two Ember versions. + * + * Returns: + * `-1` if version1 < version + * 0 if version1 == version2 + * 1 if version1 > version2 + * + * @param {String} version1 + * @param {String} version2 + * @return {Boolean} result of the comparison + */ + function compareVersion(version1, version2) { + let compared, i; + version1 = cleanupVersion(version1).split('.'); + version2 = cleanupVersion(version2).split('.'); + for (i = 0; i < 3; i++) { + compared = compare(+version1[i], +version2[i]); + if (compared !== 0) { + return compared; + } + } + return 0; + } + + /** + * Remove -alpha, -beta, etc from versions + * + * @param {String} version + * @return {String} The cleaned up version + */ + function cleanupVersion(version) { + return version.replace(/-.*/g, ''); + } + + /** + * @method compare + * @param {Number} val + * @param {Number} number + * @return {Number} + * 0: same + * -1: < + * 1: > + */ + function compare(val, number) { + if (val === number) { + return 0; + } else if (val < number) { + return -1; + } else if (val > number) { + return 1; + } + } + }(currentAdapter)); diff --git a/skeletons/web-extension/content-script.js b/skeletons/web-extension/content-script.js index 4cd952141a..fabac55719 100644 --- a/skeletons/web-extension/content-script.js +++ b/skeletons/web-extension/content-script.js @@ -61,8 +61,6 @@ document.documentElement.dataset.emberExtension = 1; } - - // Iframes should not reset the icon so we make sure // it's the top level window before resetting. if (window.top === window) { @@ -70,7 +68,6 @@ chrome.runtime.sendMessage({ type: 'resetEmberIcon' }); } - /** * Inject JS into the page to check for an app on domready. The in-page-script * is used by all variants of ember-inspector (Chrome, FF, Bookmarklet) to get @@ -91,7 +88,7 @@ */ var iframes = document.getElementsByTagName('iframe'); var urls = []; - for (var i = 0, l = iframes.length; i < l; i ++) { + for (var i = 0, l = iframes.length; i < l; i++) { urls.push(iframes[i].src); } @@ -103,6 +100,4 @@ setTimeout(function() { chrome.runtime.sendMessage({type: 'iframes', urls: urls}); }, 500); - - -}()); +})(); diff --git a/skeletons/web-extension/manifest.json b/skeletons/web-extension/manifest.json index f667bf569f..8ef825084c 100644 --- a/skeletons/web-extension/manifest.json +++ b/skeletons/web-extension/manifest.json @@ -18,7 +18,7 @@ "contextMenus" ], - "content_security_policy": "script-src 'self'; object-src 'self'", + "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'", "devtools_page": "devtools.html", "content_scripts": [{