From bce9f260c66c7b4399d85eddc38c8d19dfa0cf43 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 17 Jan 2021 18:34:35 +0100 Subject: Set up Native Messaging and reload extensions Get the extension IDs from the Native Messaging host and reload the specified extensions. Also reload the current tab. Turns out this doesn't work the way I expected. It does disable and re-enable the extension, but it doesn't reload the extension as with `chrome.runtime.reload()`. This means the specified extension isn't reloaded with the latest code changes. Unfortunately, it looks like there's no API to do what I want, and unless there's some magic in the `chrome.debugger` API I'll have to give up on this project. --- extension/background.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 extension/background.js (limited to 'extension/background.js') diff --git a/extension/background.js b/extension/background.js new file mode 100644 index 0000000..fa7defb --- /dev/null +++ b/extension/background.js @@ -0,0 +1,31 @@ +var NATIVE_HOST_ID = 'com.teddywing.extreload'; +var port = null; + +port = chrome.runtime.connectNative(NATIVE_HOST_ID); +port.onMessage.addListener(on_native_message); +port.onDisconnect.addListener(on_disconnected); + +function on_native_message(message) { + console.log(message); + + if (message.ids) { + message.ids.forEach(function(id) { + // Disable extension + chrome.management.setEnabled(id, false, function() { + console.log('Disabled', id); + + // Enable extension + chrome.management.setEnabled(id, true, function() { + console.log('Enabled', id); + + // Reload the current tab + chrome.tabs.reload(); + }); + }); + }); + } +} + +function on_disconnected() { + console.warn('Native host disconnected'); +} -- cgit v1.2.3