diff options
author | kumavis <kumavis@users.noreply.github.com> | 2016-07-26 09:37:10 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-26 09:37:10 +0800 |
commit | 04f755a132e80fc018e42e2104f6c6feffeb183f (patch) | |
tree | c3f1e7a428cff016caa214712d49dead45ebf335 /app/scripts | |
parent | 11dfb8e869d425204512b250b00ef71ed85a14cc (diff) | |
parent | 0eb7354c1acc036452115351485d0fbc94b4bb08 (diff) | |
download | tangerine-wallet-browser-04f755a132e80fc018e42e2104f6c6feffeb183f.tar tangerine-wallet-browser-04f755a132e80fc018e42e2104f6c6feffeb183f.tar.gz tangerine-wallet-browser-04f755a132e80fc018e42e2104f6c6feffeb183f.tar.bz2 tangerine-wallet-browser-04f755a132e80fc018e42e2104f6c6feffeb183f.tar.lz tangerine-wallet-browser-04f755a132e80fc018e42e2104f6c6feffeb183f.tar.xz tangerine-wallet-browser-04f755a132e80fc018e42e2104f6c6feffeb183f.tar.zst tangerine-wallet-browser-04f755a132e80fc018e42e2104f6c6feffeb183f.zip |
Merge pull request #482 from MetaMask/FirefoxCompatibility
Fix inpage script race condition
Diffstat (limited to 'app/scripts')
-rw-r--r-- | app/scripts/contentscript.js | 33 | ||||
-rw-r--r-- | app/scripts/lib/extension-instance.js | 25 |
2 files changed, 44 insertions, 14 deletions
diff --git a/app/scripts/contentscript.js b/app/scripts/contentscript.js index 0ffe93e3c..1eb04059d 100644 --- a/app/scripts/contentscript.js +++ b/app/scripts/contentscript.js @@ -3,19 +3,37 @@ const PortStream = require('./lib/port-stream.js') const ObjectMultiplex = require('./lib/obj-multiplex') const extension = require('./lib/extension') +const fs = require('fs') +const path = require('path') +const inpageText = fs.readFileSync(path.join(__dirname + '/inpage.js')).toString() + +// Eventually this streaming injection could be replaced with: +// https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Language_Bindings/Components.utils.exportFunction +// +// But for now that is only Firefox +// If we create a FireFox-only code path using that API, +// MetaMask will be much faster loading and performant on Firefox. + if (shouldInjectWeb3()) { setupInjection() setupStreams() } function setupInjection(){ - // inject in-page script - var scriptTag = document.createElement('script') - scriptTag.src = extension.extension.getURL('scripts/inpage.js') - scriptTag.onload = function () { this.parentNode.removeChild(this) } - var container = document.head || document.documentElement - // append as first child - container.insertBefore(scriptTag, container.children[0]) + try { + + // inject in-page script + var scriptTag = document.createElement('script') + scriptTag.src = extension.extension.getURL('scripts/inpage.js') + scriptTag.textContent = inpageText + scriptTag.onload = function () { this.parentNode.removeChild(this) } + var container = document.head || document.documentElement + // append as first child + container.insertBefore(scriptTag, container.children[0]) + + } catch (e) { + console.error('Metamask injection failed.', e) + } } function setupStreams(){ @@ -44,7 +62,6 @@ function setupStreams(){ pluginStream.on('close', function () { reloadStream.write({ method: 'reset' }) }) - } function shouldInjectWeb3(){ diff --git a/app/scripts/lib/extension-instance.js b/app/scripts/lib/extension-instance.js index eeab6c6d0..eb3b8a1e9 100644 --- a/app/scripts/lib/extension-instance.js +++ b/app/scripts/lib/extension-instance.js @@ -24,14 +24,27 @@ const apis = [ function Extension () { const _this = this - let global = window - - if (window.chrome) { - global = window.chrome - } apis.forEach(function (api) { - _this[api] = global[api] + + _this[api] = null + + try { + if (chrome[api]) { + _this[api] = chrome[api] + } + } catch (e) {} + + try { + if (window[api]) { + _this[api] = window[api] + } + } catch (e) {} + + try { + _this.api = browser.extension[api] + } catch (e) {} + }) } |