Skip to content

Commit

Permalink
Merge pull request #1496 from gamebeaker/change-403-logic-(block-futu…
Browse files Browse the repository at this point in the history
…re-requests-for-site)

Change 403 user options/ handling
  • Loading branch information
gamebeaker authored Sep 20, 2024
2 parents de1ab17 + 72e0e1e commit 3314747
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
10 changes: 9 additions & 1 deletion plugin/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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": {
Expand Down
21 changes: 21 additions & 0 deletions plugin/js/ErrorLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}

Expand Down
22 changes: 19 additions & 3 deletions plugin/js/HttpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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};
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -240,6 +254,8 @@ class HttpClient {
}
}

let BlockedHostNames = new Set();

class FetchResponseHandler {
isHtml() {
return this.contentType.startsWith("text/html");
Expand Down
2 changes: 2 additions & 0 deletions plugin/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
<button class="expandedButton" id="errorButtonOk">__MSG_button_error_OK__</button>
<button class="expandedButton" id="errorButtonRetry">__MSG_button_error_Retry__</button>
<button class="expandedButton" id="errorButtonCancel">__MSG_button_error_Cancel__</button>
<button class="expandedButton" id="errorButtonOpenURL">__MSG_button_error_Open_URL__</button>
<button class="expandedButton" id="errorButtonBlockURL">__MSG_button_error_Block_URL__</button>
</td>
</tr>
<tr id="errorMessageRow">
Expand Down

0 comments on commit 3314747

Please sign in to comment.