From 72e0e1eb632e28819592c5d41c68cc7df97577a3 Mon Sep 17 00:00:00 2001 From: gamebeaker Date: Fri, 20 Sep 2024 17:09:26 +0200 Subject: [PATCH] Change 403 user options/ handling --- plugin/_locales/en/messages.json | 10 +++++++++- plugin/js/ErrorLog.js | 21 +++++++++++++++++++++ plugin/js/HttpClient.js | 22 +++++++++++++++++++--- plugin/popup.html | 2 ++ 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/plugin/_locales/en/messages.json b/plugin/_locales/en/messages.json index 5703f931..5ea8862e 100644 --- a/plugin/_locales/en/messages.json +++ b/plugin/_locales/en/messages.json @@ -115,6 +115,14 @@ "message": "Retry", "description": "Label on 'Retry' button when show error" }, + "__MSG_button_error_Open_URL__": { + "message": "Open URL for Captcha", + "description": "Label on 'Open URL for Captcha' button when show error HTTP 403" + }, + "__MSG_button_error_Block_URL__": { + "message": "Block Website for future requests", + "description": "Label on 'Block Website for future requests' button when show error HTTP 403" + }, "__MSG_button_finished_default_parser__": { "message": "Apply", "description": "Label on button to finish configuring default parser" @@ -635,7 +643,7 @@ "description": "Internal message for developer." }, "warning403ErrorResponse": { - "message": "WARNING: Site '$host$' has sent an Access Denied (403) error. You may need to logon to site, or browse site normally until you get a Cloudflare \"Are you a human\" page or satisfy some other CAPTCHA before WebToEpub can continue. Open Page?", + "message": "WARNING: Site '$host$' has sent an Access Denied (403) error.\nYou may need to logon to site, or browse site normally\nuntil you get a Cloudflare \"Are you a human\" page or satisfy some other CAPTCHA\nbefore WebToEpub can continue.\n", "description": "Warning message for user when site sends a 403 response.", "placeholders": { "host": { diff --git a/plugin/js/ErrorLog.js b/plugin/js/ErrorLog.js index 60ca1f1b..57504624 100644 --- a/plugin/js/ErrorLog.js +++ b/plugin/js/ErrorLog.js @@ -89,6 +89,8 @@ class ErrorLog { let okButton = document.getElementById("errorButtonOk"); let retryButton = document.getElementById("errorButtonRetry"); let cancelButton = document.getElementById("errorButtonCancel"); + let OpenURLButton = document.getElementById("errorButtonOpenURL"); + let BlockURLButton = document.getElementById("errorButtonBlockURL"); if (msg.retryAction !== undefined) { okButton.hidden = true; retryButton.hidden = false; @@ -105,11 +107,30 @@ class ErrorLog { if (msg.cancelLabel !== undefined) { cancelButton.textContent = msg.cancelLabel; }; + if (msg.openurl !== undefined) { + OpenURLButton.hidden = false; + OpenURLButton.onclick = function() { + //window.open(new URL(msg.openurl), "_blank").focus(); + //use chrome.tabs.create to prevent auto popup block from browser + chrome.tabs.create({ url: msg.openurl}); + }; + BlockURLButton.hidden = false; + BlockURLButton.onclick = function() { + close(); + BlockedHostNames.add(new URL(msg.blockurl).hostname); + msg.cancelAction(); + }; + } else { + OpenURLButton.hidden = true; + BlockURLButton.hidden = true; + } } else { okButton.hidden = false; okButton.onclick = close; retryButton.hidden = true; cancelButton.hidden = true; + OpenURLButton.hidden = true; + BlockURLButton.hidden = true; } } diff --git a/plugin/js/HttpClient.js b/plugin/js/HttpClient.js index 871eccca..0e12978b 100644 --- a/plugin/js/HttpClient.js +++ b/plugin/js/HttpClient.js @@ -52,9 +52,18 @@ class FetchErrorHandler { } promptUserForRetry(url, wrapOptions, response, failError) { - let msg = new Error(new Error(this.makeFailCanRetryMessage(url, response.status))); + let msg; + if (wrapOptions.retry.HTTP === 403) { + msg = new Error(chrome.i18n.getMessage("warning403ErrorResponse", new URL(response.url).hostname) + this.makeFailCanRetryMessage(url, response.status)); + } else { + msg = new Error(new Error(this.makeFailCanRetryMessage(url, response.status))); + } let cancelLabel = this.getCancelButtonText(); return new Promise(function(resolve, reject) { + if (wrapOptions.retry.HTTP === 403) { + msg.openurl = url; + msg.blockurl = url; + } msg.retryAction = () => resolve(HttpClient.wrapFetchImpl(url, wrapOptions)); msg.cancelAction = () => reject(failError); msg.cancelLabel = cancelLabel; @@ -73,14 +82,15 @@ class FetchErrorHandler { let retryDelay = [120, 60, 30, 15]; switch(response.status) { case 403: + /* if (confirm(chrome.i18n.getMessage("warning403ErrorResponse", new URL(response.url).hostname))) { // Open site window.open(new URL(response.url), "_blank").focus(); alert(chrome.i18n.getMessage("wait403ErrorResponse", new URL(response.url).hostname)); } else { // Do nothing! - } - return {retryDelay: [1], promptUser: true}; + }*/ + return {retryDelay: [1], promptUser: true, HTTP: 403}; case 429: FetchErrorHandler.show429Error(response); return {retryDelay: retryDelay, promptUser: true}; @@ -178,6 +188,10 @@ class HttpClient { } static async wrapFetchImpl(url, wrapOptions) { + if (BlockedHostNames.has(new URL(url).hostname)) { + let skipurlerror = new Error("!Blocked! URL skipped because the user blocked the site"); + return wrapOptions.errorHandler.onFetchError(url, skipurlerror); + } await HttpClient.setPartitionCookies(url); if (wrapOptions.fetchOptions == null) { wrapOptions.fetchOptions = HttpClient.makeOptions(); @@ -240,6 +254,8 @@ class HttpClient { } } +let BlockedHostNames = new Set(); + class FetchResponseHandler { isHtml() { return this.contentType.startsWith("text/html"); diff --git a/plugin/popup.html b/plugin/popup.html index c2adf681..43b43943 100644 --- a/plugin/popup.html +++ b/plugin/popup.html @@ -23,6 +23,8 @@ + +