From 87280c2de5f06ce5947f606cee5d694b9525d9b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20B=C3=A9matol?= Date: Fri, 20 Dec 2024 14:11:30 +0100 Subject: [PATCH] BDE-240: Inject content script into the page to detect disconnection --- src/main/content-script.js | 22 ++++++++++++++++++++++ src/main/manifest-chrome.json | 11 ++++++++++- vite.config.js | 9 +++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/main/content-script.js diff --git a/src/main/content-script.js b/src/main/content-script.js new file mode 100644 index 0000000..a6b9b40 --- /dev/null +++ b/src/main/content-script.js @@ -0,0 +1,22 @@ +(async () => { + let connected; + + try { + const response = await fetch('/nuxeo/json/cmis'); + // Some pages can return an HTML page with 404 message on it. + // We should validate that the response is a json object. + await response.json(); + connected = response.ok; + } catch (error) { + connected = false; + } + + if (!connected) { + await chrome.runtime.sendMessage({ + extension: 'nuxeo-web-extension', + component: 'serverConnector', + action: 'disconnect', + params: [] + }); + } +})(); \ No newline at end of file diff --git a/src/main/manifest-chrome.json b/src/main/manifest-chrome.json index f1889ee..909c804 100644 --- a/src/main/manifest-chrome.json +++ b/src/main/manifest-chrome.json @@ -42,5 +42,14 @@ }, "default_title": "Nuxeo Dev Tools", "default_popup": "popup/index.html" - } + }, + "content_scripts": [ + { + "matches": [ + "http://*/*", + "https://*/*" + ], + "js": ["content-script.js"] + } + ] } diff --git a/vite.config.js b/vite.config.js index da6636e..911db22 100644 --- a/vite.config.js +++ b/vite.config.js @@ -92,6 +92,15 @@ export default defineConfig(({ mode }) => { ], hook: 'writeBundle', // run the plugin at the end of bundling }), + copy({ + targets: [ + { + src: 'src/main/content-script.js', + dest: `dist/${browserVendor}`, + } + ], + // hook: 'writeBundle', // run the plugin at the end of bundling + }), htmlReparent({ browserVendor, buildEntry, sourceDir: 'src', outputDir: `dist/${browserVendor}` }), mainReparent({ browserVendor, buildEntry, sourceDir: 'src', outputDir: `dist/${browserVendor}` }), astPatch({ browserVendor, buildEntry }),