Skip to content

Commit

Permalink
(#3) Limit autochecks to URLs specified by the server only
Browse files Browse the repository at this point in the history
  • Loading branch information
Difegue committed May 7, 2021
1 parent a61bc68 commit 5c99192
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
21 changes: 17 additions & 4 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,36 @@ chrome.runtime.onMessage.addListener(
// Reverify tab req
if (request.type == "recheckTab") {
tabHashmap.delete(request.tab.id);
onNewUrl(request.tab);
onNewUrl(request.tab, true);
}

});


function onNewUrl(tab) {
function onNewUrl(tab, bypassRegexes = false) {
// If the tab already has a browserAction, we do nothing
if (!tabHashmap.has(tab.id))
chrome.storage.sync.get(['server', 'api'], function (result) {
chrome.storage.sync.get(['server', 'api', 'supportedURLs'], function (result) {
if (typeof result.server !== 'undefined' && result.server.trim() !== "") // check for undefined
checkUrl(result.server.trim(), result.api, tab);
if (bypassRegexes || isUrlSupported(tab, result.supportedURLs))
checkUrl(result.server.trim(), result.api, tab);
else
updateTabInfo(tab, { status: "other", message: "This website is not supported for downloading. But you can still try it!" });
else
updateTabInfo(tab, { status: "other", message: "Please setup your server settings." });
});
}

function isUrlSupported(tab, urlRegexes) {

if (typeof urlRegexes === 'undefined' || tab.url === undefined) return false;

urlRegexes = urlRegexes.map(s => new RegExp(s));
let checks = urlRegexes.map(regex => regex.test(tab.url));

return (checks.includes(true));
}

/**
* Send a call to the source finder to check if the given URL is already downloaded or not.
* Updates the badge with the results
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Tsukihi",
"version": "0.8",
"version": "0.9",
"description": "Send URLs to your LANraragi server for downloading! 🐦",
"permissions": [
"activeTab",
Expand Down
4 changes: 4 additions & 0 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ <h2>Server settings</h2>
<h2>Download settings</h2>
</td>
</tr>
<tr>
<td>URL schemas downloadable by the server:</td>
<td><pre id="urlRegexes"></pre></td>
</tr>
<tr>
<td>Downloaded URLs will be <br>added to this Category:</td>
<td>
Expand Down
21 changes: 16 additions & 5 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

'use strict';

let supportedUrls = [];

function getAuthHeader(apiKey) {
return { Authorization: "Bearer " + btoa(apiKey) };
}
Expand All @@ -18,7 +20,8 @@ function updateSettings() {
chrome.storage.sync.set({
server: server.value,
api: key.value,
categoryID: catid.value
categoryID: catid.value,
supportedURLs: supportedUrls
}, function () {
status.textContent = "πŸ‘Œ Saved!"
});
Expand All @@ -33,6 +36,7 @@ function checkServer() {

let serverInfo = "";
let apiCheck = "";
let urlRegexText = "";

// Test base server connectivity
fetch(`${server.value}/api/info`, { method: "GET", headers: getAuthHeader(key.value) })
Expand All @@ -48,18 +52,25 @@ function checkServer() {
.catch(error => { serverInfo = `❌ ${error}` })
.finally(() => document.getElementById('serverMsg').textContent = serverInfo);

// Test authenticated endpoint
fetch(`${server.value}/api/plugins/test`, { method: "GET", headers: getAuthHeader(key.value) })
// Test authenticated endpoint by getting Download plugins
fetch(`${server.value}/api/plugins/download`, { method: "GET", headers: getAuthHeader(key.value) })
.then(response => response.ok ? response.json() : { error: "API Key seems to be invalid!" })
.then((data) => {

if (data.error)
throw new Error(data.error);

apiCheck = `βœ… API Key is valid.`;

supportedUrls = data.map(plug => plug.url_regex)
urlRegexText = supportedUrls.join('\r\n');

})
.catch(error => { apiCheck = `πŸ›‘ ${error}` })
.finally(() => document.getElementById('apiCheck').textContent = apiCheck);
.catch(error => { apiCheck = urlRegexText = `πŸ›‘ ${error}` })
.finally(() => {
document.getElementById('apiCheck').textContent = apiCheck;
document.getElementById('urlRegexes').textContent = urlRegexText;
});

// Get categories from the server
document.getElementById('categoryErr').textContent = "";
Expand Down

0 comments on commit 5c99192

Please sign in to comment.