diff --git a/extension/_locales/en/messages.json b/extension/_locales/en/messages.json --- a/extension/_locales/en/messages.json +++ b/extension/_locales/en/messages.json @@ -98,6 +98,15 @@ "message": "Adds a \"Share...\" context menu entry and allows websites to open a dialog for sharing contents using the Web Share API." }, + "options_plugin_captiveportal_title": { + "description": "Title for Captive Portal plugin", + "message": "Improved Captive Portal Handling" + }, + "options_plugin_captiveportal_description": { + "description": "Description for Captive Portal plugin", + "message": "When a successful login to a captive portal is detected, network connectivity status is automatically refreshed." + }, + "options_plugin_breezeScrollBars_title": { "description": "Title for Breeze style scroll bars plugin", "message": "Use Breeze-style scroll bars" diff --git a/extension/constants.js b/extension/constants.js --- a/extension/constants.js +++ b/extension/constants.js @@ -37,6 +37,9 @@ purpose: { enabled: true }, + captiveportal: { + enabled: true + }, breezeScrollBars: { // this breaks pages in interesting ways, disable by default enabled: false @@ -52,3 +55,5 @@ const MPRIS_WEBSITE_SETTINGS = { //"https://www.example.com": false }; + +const CAPTIVE_PORTAL_NETWORKCHECK_URL = "http://networkcheck.kde.org/"; diff --git a/extension/extension-captiveportal.js b/extension/extension-captiveportal.js new file mode 100644 --- /dev/null +++ b/extension/extension-captiveportal.js @@ -0,0 +1,75 @@ +/* + Copyright (C) 2019 Kai Uwe Broulik + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 3 of + the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +let captivePortalTabId = -1; +let delayCaptiveCheckTimeout = null; + +chrome.tabs.onCreated.addListener((tab) => { + if (tab.url !== CAPTIVE_PORTAL_NETWORKCHECK_URL) { + return; + } + + sendPortMessageWithReply("captiveportal", "getConnectivity").then((reply) => { + if (reply.connectivity === "PORTAL") { + console.log("We're behind a captive portal, tracking tab", tab.id); + captivePortalTabId = tab.id; + } + }); +}); + +chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { + if (captivePortalTabId === -1 || tabId !== captivePortalTabId) { + return; + } + + if (changeInfo.status !== "complete") { + return; + } + + // if we're on KDE networkcheck page and it returns "OK", + // close the tab as we're connected successfully + if (tab.url === CAPTIVE_PORTAL_NETWORKCHECK_URL) { + chrome.tabs.executeScript(tab.id, { + code: `document.body.textContent == "OK\\n"` + }, (result) => { + console.log("Successfully navigated to networkcheck page, closing it"); + chrome.tabs.remove(tab.id); + }); + } + + // Delay checking for connectivity a bit to avoid excess checks in case of multiple redirects + if (delayCaptiveCheckTimeout) { + return; + } + + delayCaptiveCheckTimeout = setTimeout(() => { + sendPortMessageWithReply("captiveportal", "checkConnectivity").then((reply) => { + delayCaptiveCheckTimeout = null; + + if (reply.connectivity === "FULL") { + console.log("We are now fully connected"); + captivePortalTabId = -1; + } + }); + }, 2000); +}); + +chrome.tabs.onRemoved.addListener((tabId) => { + if (tabId === captivePortalTabId) { + captivePortalTabId = -1; + } +}); diff --git a/extension/manifest.json b/extension/manifest.json --- a/extension/manifest.json +++ b/extension/manifest.json @@ -30,6 +30,7 @@ "extension-downloads.js", "extension-tabsrunner.js", "extension-purpose.js", + "extension-captiveportal.js", "extension.js" ], diff --git a/extension/options.html b/extension/options.html --- a/extension/options.html +++ b/extension/options.html @@ -83,6 +83,12 @@

I18N

+
  • + +

    I18N

    +