wip refactoring

This commit is contained in:
Jack Case
2025-11-05 21:33:32 +00:00
parent f6bfb1aa4d
commit 83bc5190a7
5 changed files with 65 additions and 36 deletions

View File

@@ -3,11 +3,12 @@
<meta charset="UTF-8">
<!-- <meta name="viewport" content="width=device-width, initial-scale=1.0"> -->
<!-- <script async defer src="https://cdn.jsdelivr.net/gh/altcha-org/altcha/dist/altcha.min.js" type="module"></script> -->
<script async src="/scripts/report-slop.js" type="module"></script>
<script async src="/scripts/action_popup.js" type="module"></script>
<title>Slop Farmer</title>
</head>
<body>
<h1>Log in to enable slop checking and reporting</h1>
<form id="login-form">
<label for="email" id="username">username</label>
<input type="text" name="username" required />
@@ -16,4 +17,6 @@
<button id="login-button">login</button>
</form>
<h2 style="visibility: collapse;" id="login-status">You're logged in.</h2>
</body>

View File

@@ -1,4 +1,4 @@
import { API_URL } from "common"
import { API_URL, send_message_to_background } from "common"
const login_form = document.getElementById("login-form") as HTMLFormElement
const login_status = document.getElementById("login-status")
@@ -22,8 +22,13 @@ async function submit_login_form() {
if (response.ok) {
const body = await response.json()
const token = body.access_token
localStorage.setItem("accessToken", token)
await send_message_to_background({type: "login", token: token})
const status_el = document.getElementById("login-status")
status_el.setAttribute("style", "visibility: visible;")
}
else {
//bad login, update the form
}
}

View File

@@ -1 +1,6 @@
export const API_URL: string = "https://api.slopfarmer.jack-case.pro"
export async function send_message_to_background(message: any): Promise<any> {
const response = await browser.runtime.sendMessage(message)
return response
}

View File

@@ -1,3 +1,5 @@
import { send_message_to_background } from "common"
class SearchLink {
node: Element
@@ -75,24 +77,26 @@ function check_links(search_links: SearchLink[]) {
search_link.checked = true
return search_link.target
})
browser.runtime.sendMessage({type: "check", urls: urls})
send_message_to_background({type: "check", urls: urls})
}
async function backend_message_listener(message: any) {
// handle slop reports returned from the background script
if(message.type === "check_result") {
if (message.domain) {
const paths = page_links.getDomain(message.domain)
paths.forEach((search_link: SearchLink) => {
search_link.node.setAttribute("style", "color: red;")
search_link.result = message.result
})
} else if (message.url) {
const link = page_links.getUrl(message.url)
link.node.setAttribute("style", "color: red;")
link.result = message.result
}
switch(message.type) {
case ("check-result"):
if (message.domain) {
const paths = page_links.getDomain(message.domain)
paths.forEach((search_link: SearchLink) => {
search_link.node.setAttribute("style", "color: red;")
search_link.result = message.result
})
} else if (message.url) {
const link = page_links.getUrl(message.url)
link.node.setAttribute("style", "color: red;")
link.result = message.result
}
break
}
}

View File

@@ -21,11 +21,13 @@ function setup_storage_db() {
db_request.onsuccess = (event) => {
// create objectstore
console.log(event)
//@ts-ignore
db = event.target.result
}
db_request.onupgradeneeded = (event) => {
console.log(event)
//@ts-ignore
db = event.target.result
const slop_store = db.createObjectStore("slop", { keyPath: "domain" })
}
@@ -41,6 +43,7 @@ async function get_slop_store(readwrite: boolean) {
const db_request = window.indexedDB.open("SlopDB", 1)
db_request.onsuccess = (event) => {
//@ts-ignore
const db = event.target.result
const transaction = db.transaction(["slop"], readwrite ? "readwrite" : undefined)
const slop_store = transaction.objectStore("slop")
@@ -59,6 +62,7 @@ async function insert_slop(domain: string, path: string, report: boolean = true)
const db_request = window.indexedDB.open("SlopDB", 1)
db_request.onsuccess = (event) => {
//@ts-ignore
db = event.target.result
const transaction = db.transaction(["slop"], "readwrite")
const slop_store = transaction.objectStore("slop")
@@ -177,29 +181,37 @@ async function update_page_action_icon(details: browser.webNavigation._OnCommitt
console.log(is_slop)
}
async function message_listener(message: any, sender: any) {
async function message_listener(message: any, sender: any, send_response: Function) {
const tabid = sender.tab.id
if (message.type === "check") {
let check_promises = new Array()
let not_found_local = new Array()
switch (message.type) {
message.urls.forEach((url: string) => {
check_promises.push(check_local_slop(url).then(async (result) => {
if (result.slop_domain) {
browser.tabs.sendMessage(tabid, { type: "check_result", url: url, result: result })
}
else {
not_found_local.push(url)
}
}))
})
case "check":
let check_promises = new Array()
let not_found_local = new Array()
await Promise.all(check_promises)
message.urls.forEach((url: string) => {
check_promises.push(check_local_slop(url).then(async (result) => {
if (result.slop_domain) {
browser.tabs.sendMessage(tabid, { type: "check_result", url: url, result: result })
}
else {
not_found_local.push(url)
}
}))
})
let remote_slop = await check_remote_slop(not_found_local)
remote_slop.forEach((result: any) => {
browser.tabs.sendMessage(tabid, { type: "check_result", domain: result.domain_name, result: result })
})
await Promise.all(check_promises)
let remote_slop = await check_remote_slop(not_found_local)
remote_slop.forEach((result: any) => {
browser.tabs.sendMessage(tabid, { type: "check_result", domain: result.domain_name, result: result })
})
break
case "login":
localStorage.setItem("accessToken", message.token)
send_response(true)
break
}
}