diff --git a/pages/action_popup.html b/pages/action_popup.html index 1fe0186..4009e61 100644 --- a/pages/action_popup.html +++ b/pages/action_popup.html @@ -3,11 +3,12 @@ - + Slop Farmer +

Log in to enable slop checking and reporting

@@ -16,4 +17,6 @@

You're logged in.

+ + \ No newline at end of file diff --git a/src/browser-action.ts b/src/browser-action.ts index 21b416d..288e4cf 100644 --- a/src/browser-action.ts +++ b/src/browser-action.ts @@ -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 + } } \ No newline at end of file diff --git a/src/common.ts b/src/common.ts index aecc406..611196d 100644 --- a/src/common.ts +++ b/src/common.ts @@ -1 +1,6 @@ export const API_URL: string = "https://api.slopfarmer.jack-case.pro" + +export async function send_message_to_background(message: any): Promise { + const response = await browser.runtime.sendMessage(message) + return response +} diff --git a/src/hide-slop.ts b/src/hide-slop.ts index 746729b..dc815a7 100644 --- a/src/hide-slop.ts +++ b/src/hide-slop.ts @@ -1,3 +1,5 @@ +import { send_message_to_background } from "common" + class SearchLink { node: Element @@ -75,25 +77,27 @@ 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 - } } diff --git a/src/report-slop.ts b/src/report-slop.ts index 74fdb5d..ff7962e 100644 --- a/src/report-slop.ts +++ b/src/report-slop.ts @@ -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) { + + case "check": + let check_promises = new Array() + let not_found_local = new Array() - 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) - } - })) - }) + 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) + } + })) + }) - await Promise.all(check_promises) + 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 }) - }) + 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 } }