diff options
author | ManeraKai <manerakai@protonmail.com> | 2024-08-29 15:29:51 +0300 |
---|---|---|
committer | ManeraKai <manerakai@protonmail.com> | 2024-08-29 15:29:51 +0300 |
commit | 1492345b0d2f0f4d6d4a7a6f8814596785a472b0 (patch) | |
tree | 420915520ff2c8bccfab7d04d3f6a6033e7ba6d8 /src/pages/background/background.js | |
parent | Merge branch 'master' of https://github.com/libredirect/browser_extension (diff) | |
download | libredirect-1492345b0d2f0f4d6d4a7a6f8814596785a472b0.zip |
Added message popup for Server Errors https://github.com/libredirect/browser_extension/issues/936
Diffstat (limited to 'src/pages/background/background.js')
-rw-r--r-- | src/pages/background/background.js | 34 |
1 files changed, 23 insertions, 11 deletions
diff --git a/src/pages/background/background.js b/src/pages/background/background.js index db7ad087..2bb6cb57 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -58,7 +58,7 @@ browser.webRequest.onBeforeRequest.addListener( return null } - const newUrl = servicesHelper.redirect( + let newUrl = servicesHelper.redirect( url, details.type, originUrl, @@ -72,20 +72,16 @@ browser.webRequest.onBeforeRequest.addListener( const frontend = url.searchParams.get("frontend") const oldUrl = new URL(url.searchParams.get("url")) - browser.tabs.update({ - url: browser.runtime.getURL( - `/pages/messages/index.html?message=no_instance&url=${encodeURIComponent(oldUrl)}&frontend=${encodeURIComponent(frontend)}` - ), - }) + newUrl = browser.runtime.getURL( + `/pages/messages/index.html?message=no_instance&url=${encodeURIComponent(oldUrl)}&frontend=${encodeURIComponent(frontend)}` + ) } if (!newUrl) { if (url.href.match(/^https?:\/{2}(.*\.)?libredirect\.invalid.*/)) { - browser.tabs.update({ - url: browser.runtime.getURL( - `/pages/messages/index.html?message=disabled&url=${encodeURIComponent(url.href)}` - ), - }) + newUrl = browser.runtime.getURL( + `/pages/messages/index.html?message=disabled&url=${encodeURIComponent(url.href)}` + ) } } @@ -108,6 +104,22 @@ browser.webRequest.onBeforeRequest.addListener( ["blocking"] ) +browser.webRequest.onHeadersReceived.addListener( + details => { + if (details.statusCode >= 501 || details.statusCode == 429 || details.statusCode == 403) { + const url = new URL(details.url) + const { service, frontend } = servicesHelper.computeFrontend(url) + if (!service) return + browser.tabs.update({ + url: browser.runtime.getURL( + `/pages/messages/index.html?message=server_error&code=${details.statusCode}=&url=${encodeURIComponent(url.href)}&frontend=${encodeURIComponent(frontend)}&service=${encodeURIComponent(service)}` + ), + }) + } + }, + { urls: ["<all_urls>"] } +) + browser.tabs.onRemoved.addListener(tabId => { if (tabIdRedirects[tabId] != undefined) { delete tabIdRedirects[tabId] |