From e0d6e3079e3113c3ce09c4d6e11cfbb9dc2f9f62 Mon Sep 17 00:00:00 2001 From: FoxRefire <155989196+FoxRefire@users.noreply.github.com> Date: Tue, 25 Jun 2024 10:43:09 +0900 Subject: [PATCH] Refactor:Cleanup code 2 --- background.js | 5 ++-- popup.html | 2 +- popup_drawList.js | 63 +++++++++++++++++++---------------------------- 3 files changed, 29 insertions(+), 41 deletions(-) diff --git a/background.js b/background.js index bc8dd00..47e43fe 100644 --- a/background.js +++ b/background.js @@ -15,10 +15,9 @@ function convertHeaders(obj){ } window.blockRules = await fetch("blockRules.conf").then((r)=>r.text()); -blockRules = blockRules.replace(/\n^\s*$|\s*\/\/.*|\s*$/gm, ""); -blockRules = blockRules.split("\n"); +window.blockRules = window.blockRules.replace(/\n^\s*$|\s*\/\/.*|\s*$/gm, "").split("\n"); function testBlock(url) { - return window.isBlock && blockRules.map(e => url.includes(e)).some(e=>e); + return window.isBlock && window.blockRules.some(e => url.includes(e)); } //Get URL and headers from POST requests diff --git a/popup.html b/popup.html index a1cff44..44e2bfe 100644 --- a/popup.html +++ b/popup.html @@ -5,7 +5,7 @@ - +


diff --git a/popup_drawList.js b/popup_drawList.js index 93df356..fb10b38 100644 --- a/popup_drawList.js +++ b/popup_drawList.js @@ -3,7 +3,6 @@ function selectPssh(){ document.getElementById('selectPssh').style.display='grid'; document.getElementById('psshList').style.display='grid'; document.getElementById('toggleHistory').style.display='none'; - document.getElementById('bodyid').style.gridTemplateRows='auto 1fr auto'; } function selectRequest(){ @@ -11,50 +10,40 @@ function selectRequest(){ document.getElementById('selectRequest').style.display='grid'; document.getElementById('requestList').style.display='grid'; document.getElementById('toggleHistory').style.display='none'; - document.getElementById('bodyid').style.gridTemplateRows='auto 1fr auto'; } document.getElementById('psshButton').addEventListener("click", selectPssh); document.getElementById('licenseButton').addEventListener("click", selectRequest); -var userInputs={}; -function drawList(arr,_searchBox,_list,_userInputs){ - const elements = arr; - const searchBox = document.getElementById(_searchBox); - const list = document.getElementById(_list); - - elements.forEach((element, index) => { - const li = document.createElement('li'); - li.textContent = element; - li.addEventListener('click', () => { - userInputs[_userInputs]=index; - document.getElementById(_userInputs).value=element; - document.getElementById(_userInputs+'Index').value=index; - document.getElementById('selectPssh').style.display='none'; - document.getElementById('selectRequest').style.display='none'; - document.getElementById('home').style.display='grid'; - document.getElementById('toggleHistory').style.display='grid'; - }); - list.appendChild(li); +function writeListElement(arrElements, list, outputVar, search) { + list.innerHTML = ''; + arrElements.forEach((element, index) => { + if (!search || element.includes(searchValue)) { + const li = document.createElement('li'); + li.textContent = element; + li.addEventListener('click', () => { + userInputs[outputVar]=index; + document.getElementById(outputVar).value=element; + document.getElementById(outputVar+'Index').value=index; + document.getElementById('selectPssh').style.display='none'; + document.getElementById('selectRequest').style.display='none'; + document.getElementById('home').style.display='grid'; + document.getElementById('toggleHistory').style.display='grid'; + }); + list.appendChild(li); + } }); +} + +var userInputs={}; +function drawList(arrElements,searchBoxElmId,listElmId,outputVar) { + const searchBox = document.getElementById(searchBoxElmId); + const list = document.getElementById(listElmId); + + writeListElement(arrElements, list, outputVar, null) searchBox.addEventListener('input', (event) => { const searchValue = event.target.value.toLowerCase(); - list.innerHTML = ''; - elements.forEach((element, index) => { - if (element.toLowerCase().includes(searchValue)) { - const li = document.createElement('li'); - li.textContent = element; - li.addEventListener('click', () => { - userInputs[_userInputs]=index; - document.getElementById(_userInputs).value=element; - document.getElementById(_userInputs+'Index').value=index; - document.getElementById('selectPssh').style.display='none'; - document.getElementById('selectRequest').style.display='none'; - document.getElementById('home').style.display='grid'; - }); - list.appendChild(li); - } - }); + writeListElement(arrElements, list, outputVar, searchValue) }); }