aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <hi@ypei.me>2022-09-26 12:55:17 +1000
committerYuchen Pei <hi@ypei.me>2022-09-26 12:55:17 +1000
commitc1f4dc70965b576920c10dc95b6c7052674d7fc6 (patch)
tree8857ea1d73fca7afd81c7854cb65a2d1ee5c0456
parent4b73c3669a0422ee4863411df9fa6d7a67850447 (diff)
refactor: remove redundant result() in checkScriptAndUpdateReport
-rw-r--r--main_background.js33
1 files changed, 14 insertions, 19 deletions
diff --git a/main_background.js b/main_background.js
index 56d9c91..030cdc2 100644
--- a/main_background.js
+++ b/main_background.js
@@ -369,13 +369,9 @@ async function onTabActivated({ tabId }) {
* Checks script and updates the report entry accordingly.
*
* Asynchronous function, returns the final edited script as a
- * string, or an array containing it and -1, if returnsString is false
+ * string.
*/
-async function checkScriptAndUpdateReport(scriptSrc, url, tabId, whitelisted, returnsString = true, isExternal = false) {
- function result(scriptSource) {
- return returnsString ? scriptSource : [scriptSource, -1];
- }
-
+async function checkScriptAndUpdateReport(scriptSrc, url, tabId, whitelisted, isExternal = false) {
const scriptName = url.split('/').pop();
if (whitelisted) {
if (tabId !== -1) {
@@ -387,15 +383,15 @@ async function checkScriptAndUpdateReport(scriptSrc, url, tabId, whitelisted, re
addReportEntry(tabId, { 'whitelisted': [site || url, reason], url });
}
if (scriptSrc.startsWith('javascript:'))
- return result(scriptSrc);
+ return scriptSrc;
else
- return result(`/* LibreJS: script whitelisted by user preference. */\n${scriptSrc}`);
+ return `/* LibreJS: script whitelisted by user preference. */\n${scriptSrc}`;
}
const [accepted, editedSource, reason] = listManager.builtInHashes.has(hash(scriptSrc)) ? [true, scriptSrc, 'Common script known to be free software.'] : checkLib.checkScriptSource(scriptSrc, scriptName, isExternal);
if (tabId < 0) {
- return result(editedSource);
+ return editedSource;
}
const domain = getDomain(url);
@@ -405,19 +401,18 @@ async function checkScriptAndUpdateReport(scriptSrc, url, tabId, whitelisted, re
switch (actionType) {
case 'blacklisted': {
const edited = `/* LibreJS: script ${actionType} by user. */`;
- return result(scriptSrc.startsWith('javascript:')
- ? `javascript:void(${encodeURIComponent(edited)})` : edited);
+ return scriptSrc.startsWith('javascript:')
+ ? `javascript:void(${encodeURIComponent(edited)})` : edited;
}
case 'whitelisted': {
- return result(scriptSrc.startsWith('javascript:')
- ? scriptSrc : `/* LibreJS: script ${actionType} by user. */\n${scriptSrc}`);
+ return scriptSrc.startsWith('javascript:')
+ ? scriptSrc : `/* LibreJS: script ${actionType} by user. */\n${scriptSrc}`;
}
default: {
const scriptSource = accepted ? scriptSrc : editedSource;
- return result(scriptSrc.startsWith('javascript:')
+ return scriptSrc.startsWith('javascript:')
? (accepted ? scriptSource : `javascript:void(/* ${scriptSource} */)`)
- : `/* LibreJS: script ${actionType}. */\n${scriptSource}`
- );
+ : `/* LibreJS: script ${actionType}. */\n${scriptSource}`;
}
}
}
@@ -549,7 +544,7 @@ var ResponseHandler = {
async function handle_script(response, whitelisted) {
const { text, request } = response;
const { url, tabId } = request;
- return await checkScriptAndUpdateReport(text, ListStore.urlItem(url), tabId, whitelisted, returnsString = true, isExternal = true);
+ return await checkScriptAndUpdateReport(text, ListStore.urlItem(url), tabId, whitelisted, isExternal = true);
}
/**
@@ -753,8 +748,8 @@ async function editHtml(html, documentUrl, tabId, frameId, whitelisted) {
editedSource = dejaVu.get(source);
} else {
let url = `view-source:${documentUrl}#line${line}(<SCRIPT>)\n${source}`;
- let edited = await checkScriptAndUpdateReport(source, url, tabId, whitelisted, returnsString = false);
- editedSource = edited && edited[0].trim();
+ let edited = await checkScriptAndUpdateReport(source, url, tabId, whitelisted);
+ editedSource = edited.trim();
dejaVu.set(url, editedSource);
}
if (editedSource) {