| OLD | NEW |
| 1 "use strict"; | 1 "use strict"; |
| 2 | 2 |
| 3 // Firefox 55 erroneously sends messages from the content script to the | 3 // Firefox 55 erroneously sends messages from the content script to the |
| 4 // devtools panel: | 4 // devtools panel: |
| 5 // https://bugzilla.mozilla.org/show_bug.cgi?id=1383310 | 5 // https://bugzilla.mozilla.org/show_bug.cgi?id=1383310 |
| 6 // As a workaround, listen for messages only if this isn't the devtools panel. | 6 // As a workaround, listen for messages only if this isn't the devtools panel. |
| 7 // Note that Firefox processes API access lazily, so browser.devtools will | 7 // Note that Firefox processes API access lazily, so browser.devtools will |
| 8 // always exist but will have undefined as its value on other pages. | 8 // always exist but will have undefined as its value on other pages. |
| 9 if (!browser.devtools) | 9 if (!browser.devtools) |
| 10 { | 10 { |
| 11 // Listen for messages from the background page. | 11 // Listen for messages from the background page. |
| 12 browser.runtime.onMessage.addListener((message, sender, sendResponse) => | 12 browser.runtime.onMessage.addListener((message, sender, sendResponse) => |
| 13 { | 13 { |
| 14 return ext.onMessage._dispatch(message, {}, sendResponse).includes(true); | 14 return ext.onMessage._dispatch(message, {}, sendResponse).includes(true); |
| 15 }); | 15 }); |
| 16 } | 16 } |
| 17 | 17 |
| 18 (function() | |
| 19 { | 18 { |
| 20 let port = null; | 19 let port = null; |
| 21 | 20 |
| 22 ext.onExtensionUnloaded = { | 21 ext.onExtensionUnloaded = { |
| 23 addListener(listener) | 22 addListener(listener) |
| 24 { | 23 { |
| 25 if (!port) | 24 if (!port) |
| 26 port = browser.runtime.connect(); | 25 port = browser.runtime.connect(); |
| 27 | 26 |
| 28 // When the extension is reloaded, disabled or uninstalled the | 27 // When the extension is reloaded, disabled or uninstalled the |
| 29 // background page dies and automatically disconnects all ports | 28 // background page dies and automatically disconnects all ports |
| 30 port.onDisconnect.addListener(listener); | 29 port.onDisconnect.addListener(listener); |
| 31 }, | 30 }, |
| 32 removeListener(listener) | 31 removeListener(listener) |
| 33 { | 32 { |
| 34 if (port) | 33 if (port) |
| 35 { | 34 { |
| 36 port.onDisconnect.removeListener(listener); | 35 port.onDisconnect.removeListener(listener); |
| 37 | 36 |
| 38 if (!port.onDisconnect.hasListeners()) | 37 if (!port.onDisconnect.hasListeners()) |
| 39 { | 38 { |
| 40 port.disconnect(); | 39 port.disconnect(); |
| 41 port = null; | 40 port = null; |
| 42 } | 41 } |
| 43 } | 42 } |
| 44 } | 43 } |
| 45 }; | 44 }; |
| 46 }()); | 45 } |
| OLD | NEW |