Restricted auto redirection to only 503, 504. Added a 'Cancel Redirection' btn

This commit is contained in:
ManeraKai 2022-03-19 00:36:18 +03:00
parent 12e2a268a8
commit 9de383439e
No known key found for this signature in database
GPG Key ID: 5ABC31FFD562E337
3 changed files with 39 additions and 10 deletions

View File

@ -130,8 +130,9 @@ browser.webRequest.onResponseStarted.addListener(
details => { details => {
if (!generalHelper.getAutoRedirect()) return null; if (!generalHelper.getAutoRedirect()) return null;
if (details.type == 'main_frame' && (details.statusCode == 503 || details.statusCode == 504)) {
// if (details.type == 'main_frame' && details.statusCode >= 200) {
console.log("statusCode", details.statusCode); console.log("statusCode", details.statusCode);
if (details.type == 'main_frame' && details.statusCode >= 500) {
const url = new URL(details.url); const url = new URL(details.url);
let newUrl; let newUrl;
@ -159,10 +160,8 @@ browser.webRequest.onResponseStarted.addListener(
// if (!newUrl) newUrl = spotifyHelper.changeInstance(url); // if (!newUrl) newUrl = spotifyHelper.changeInstance(url);
if (newUrl) { if (newUrl) browser.tabs.update(details.tabId, { url: `/pages/errors/instance_offline.html?url=${encodeURIComponent(newUrl)}` });
browser.tabs.update({ url: '/pages/errors/instance_offline.html' });
setTimeout(() => browser.tabs.update({ url: newUrl }), 2000);
}
} }
}, },
{ urls: ["<all_urls>"], } { urls: ["<all_urls>"], }

View File

@ -9,19 +9,31 @@
<link href="../stylesheets/styles.css" rel="stylesheet" /> <link href="../stylesheets/styles.css" rel="stylesheet" />
<style> <style>
body { body {
margin: 0;
padding: 0;
height: 100vh; height: 100vh;
width: 100vw; width: 100vw;
display: flex; flex-wrap: wrap;
justify-content: center; justify-content: center;
align-items: center; align-items: center;
font-size: 30px; font-size: 30px;
display: flex;
}
div {
text-align: center;
} }
</style> </style>
</head> </head>
<body> <body>
This instance is offline, you'll be redirected after&nbsp;<span id="number">2</span>&nbsp;seconds <div>
<p id="message">This instance is offline, you'll be redirected after&nbsp;<span
id="number">2</span>&nbsp;seconds
</p>
<button id="cancel">Cancel</button>
</div>
<script src="instance_offline.js"></script> <script src="instance_offline.js"></script>
</body> </body>

View File

@ -1,2 +1,20 @@
setTimeout(() => document.getElementById("number").innerHTML = '1', 1000); const params = new Proxy(
setTimeout(() => document.getElementById("number").innerHTML = '0', 2000); new URLSearchParams(window.location.search),
{ get: (searchParams, prop) => searchParams.get(prop) }
);
let number = document.getElementById("number");
setTimeout(() => number.innerHTML = '1', 1000);
setTimeout(
() => {
number.innerHTML = '0'
if (!isCanceled) window.location = params.url;
},
2000
);
let isCanceled = false;
document.getElementById("cancel").addEventListener("click", () => {
isCanceled = true;
document.getElementById("message").innerHTML = "Redirection Canceled";
})