Fix chrome.devtools not available in devtools extensions
authorCheng Zhao <zcbenz@gmail.com>
Mon, 23 May 2016 07:04:10 +0000 (16:04 +0900)
committerCheng Zhao <zcbenz@gmail.com>
Mon, 23 May 2016 07:04:37 +0000 (16:04 +0900)
lib/renderer/inspector.js

index ca60c84d4be9af7aaa28538c1a4ab6ca26d28107..12d3ae9072435d42d9c521e85be760758849ced5 100644 (file)
@@ -1,4 +1,7 @@
 window.onload = function () {
+  // Make sure |window.chrome| is defined for devtools extensions.
+  hijackSetInjectedScript(InspectorFrontendHost)
+
   // Use menu API to show context menu.
   window.InspectorFrontendHost.showContextMenuAtPoint = createMenu
 
@@ -6,6 +9,18 @@ window.onload = function () {
   window.WebInspector.createFileSelectorElement = createFileSelectorElement
 }
 
+const hijackSetInjectedScript = function (InspectorFrontendHost) {
+  const {setInjectedScriptForOrigin} = InspectorFrontendHost
+  InspectorFrontendHost.setInjectedScriptForOrigin = function (origin, source) {
+    const wrapped = `(function (...args) {
+      window.chrome = {}
+      const original = ${source}
+      original(...args)
+    })`
+    setInjectedScriptForOrigin(origin, wrapped)
+  }
+}
+
 var convertToMenuTemplate = function (items) {
   var fn, i, item, len, template
   template = []