wip refactoring
This commit is contained in:
@@ -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>
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user