Skip to content

Commit

Permalink
Performance and code improvements
Browse files Browse the repository at this point in the history
Removed redundancy
  • Loading branch information
BERNARDO31P committed Jun 24, 2024
1 parent df5e535 commit 630c67d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
5 changes: 4 additions & 1 deletion app/assets/js/pages/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,15 @@ tools.bindEvent("click", ".listBox .clear-button:not([aria-disabled='true'])", (
/**
* Adds the clipboard text as a URL to the list.
*/
tools.bindEvent("click", ".input .paste-button:not([aria-disabled='true'])", () => {
tools.bindEvent("click", ".input .paste-button:not([aria-disabled='true'])", (e) => {
let clipboardText = clipboard.readText();

if (!clipboardText) {
showNotification(tools.languageDB["js"]["noClipboard"], tools.languageDB["js"]["error"]);
} else tools.addUrlToList(clipboardText);

let input = e.target.closest(".input").querySelector("input");
input.value = "";
});

/**
Expand Down
23 changes: 15 additions & 8 deletions app/assets/js/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,18 @@ export function addUrlToList(url = "") {
return false;
}

let youtube = false;

let valid = false;
for (const domain of generateDomainVariations(url)) {
if (domain === "youtube") {
url = match(url, "http(?:s?):\\/\\/(?:www\\.|music\\.)?youtu(?:be\\.com\\/watch\\?v=|be\\.com\\/playlist\\?list=|\\.be\\/)([\\w\\-\\_]*)(&(amp;)?‌​[\\w\\?‌​=]*)?");

valid = true;
youtube = true;
break;
}

if (extractors.includes(domain)) {
valid = true;
break;
Expand All @@ -342,18 +352,15 @@ export function addUrlToList(url = "") {

if (urlList.indexOf(url.toString()) !== -1) {
showNotification(languageDB["js"]["urlInList"], languageDB["js"]["error"]);

return false;
}

url = url.toString();

let li = document.createElement("li"), found = null;
if ((found = match(url, "http(?:s?):\\/\\/(?:www\\.|music\\.)?youtu(?:be\\.com\\/watch\\?v=|be\\.com\\/playlist\\?list=|\\.be\\/)([\\w\\-\\_]*)(&(amp;)?‌​[\\w\\?‌​=]*)?")) !== null) {
url = found;

worker.postMessage({type: "checkPremium", url: found, mode: getCookie("mode")});
if (youtube) {
worker.postMessage({type: "checkPremium", url: url, mode: getCookie("mode")});
}

const li = document.createElement("li");
li.textContent = url;
li.setAttribute("data-url", url);

Expand Down Expand Up @@ -459,7 +466,7 @@ function removeOpacityNotification(notification) {
// TODO: Comment
function match(string, regex) {
let regExp = new RegExp(regex, "gi");
let found = string.match(regExp);
let found = string.toString().match(regExp);

return (found) ? found[0] : null;
}
Expand Down

0 comments on commit 630c67d

Please sign in to comment.