get rid of page action, start working on browser action popup
This commit is contained in:
@@ -1,15 +1,6 @@
|
||||
const API_URL: string = "https://api.slopfarmer.jack-case.pro"
|
||||
import { API_URL } from "common"
|
||||
let access_token: string
|
||||
|
||||
const login_form = document.getElementById("login-form") as HTMLFormElement
|
||||
if(login_form) {
|
||||
const login_status = document.getElementById("login-status")
|
||||
if (localStorage.getItem("accessToken")) {
|
||||
login_status.setAttribute("style", "visibility: visible;")
|
||||
}
|
||||
login_form.addEventListener("submit", (event) => {event.preventDefault(); submit_login_form()})
|
||||
}
|
||||
|
||||
function setup_storage_db() {
|
||||
/* create indexeddb object store to retain objects in the form of
|
||||
* {"domain": "domain.tld",
|
||||
@@ -18,7 +9,7 @@ function setup_storage_db() {
|
||||
* "page/2"
|
||||
* ]
|
||||
* }
|
||||
*/
|
||||
*/
|
||||
let db
|
||||
const db_request = window.indexedDB.open("SlopDB", 1)
|
||||
|
||||
@@ -36,7 +27,7 @@ function setup_storage_db() {
|
||||
db_request.onupgradeneeded = (event) => {
|
||||
console.log(event)
|
||||
db = event.target.result
|
||||
const slop_store = db.createObjectStore("slop", {keyPath: "domain"})
|
||||
const slop_store = db.createObjectStore("slop", { keyPath: "domain" })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +51,7 @@ async function get_slop_store(readwrite: boolean) {
|
||||
}
|
||||
})
|
||||
|
||||
return await slop_store_promise
|
||||
return await slop_store_promise
|
||||
}
|
||||
|
||||
async function insert_slop(domain: string, path: string, report: boolean = true) {
|
||||
@@ -80,12 +71,12 @@ async function insert_slop(domain: string, path: string, report: boolean = true)
|
||||
// domain exists, add this path
|
||||
result.paths.add(path)
|
||||
}
|
||||
|
||||
|
||||
else {
|
||||
// create a new domain object
|
||||
const paths_set = new Set()
|
||||
paths_set.add(path)
|
||||
result = {domain: domain, paths: paths_set}
|
||||
result = { domain: domain, paths: paths_set }
|
||||
}
|
||||
|
||||
// persist to indexeddb
|
||||
@@ -96,16 +87,16 @@ async function insert_slop(domain: string, path: string, report: boolean = true)
|
||||
}
|
||||
}
|
||||
|
||||
if(report) {
|
||||
if (report) {
|
||||
const report_url = new URL("/report", API_URL)
|
||||
const request = new Request(report_url,
|
||||
const request = new Request(report_url,
|
||||
{
|
||||
method: "POST",
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Content-Type": "application/json",
|
||||
"Bearer": get_access_token()
|
||||
},
|
||||
body: JSON.stringify({slop_urls: [new URL(path, "http://"+domain).toString()]})
|
||||
},
|
||||
body: JSON.stringify({ slop_urls: [new URL(path, "http://" + domain).toString()] })
|
||||
})
|
||||
fetch(request)
|
||||
}
|
||||
@@ -125,7 +116,7 @@ async function check_local_slop(url: string) {
|
||||
})
|
||||
|
||||
const slop_object = await known_slop
|
||||
let result = {slop_domain: false, slop_path: false}
|
||||
let result = { slop_domain: false, slop_path: false }
|
||||
if (slop_object) {
|
||||
// domain was found
|
||||
result.slop_domain = true
|
||||
@@ -140,10 +131,10 @@ async function check_local_slop(url: string) {
|
||||
|
||||
async function check_remote_slop(urls: string[]) {
|
||||
const check_url = new URL("/check", API_URL)
|
||||
const request = new Request(check_url, {method: "POST", headers: { "Content-Type": "application/json", "Bearer": get_access_token() }, body: JSON.stringify({slop_urls: urls})})
|
||||
const request = new Request(check_url, { method: "POST", headers: { "Content-Type": "application/json", "Bearer": get_access_token() }, body: JSON.stringify({ slop_urls: urls }) })
|
||||
const response = await fetch(request)
|
||||
let domain_objects = await response.json()
|
||||
domain_objects.forEach((domain: any) => {insert_slop(domain.domain_name, "/", false)})
|
||||
domain_objects.forEach((domain: any) => { insert_slop(domain.domain_name, "/", false) })
|
||||
return domain_objects
|
||||
}
|
||||
|
||||
@@ -157,21 +148,21 @@ async function on_button_clicked_handler(tab: any) {
|
||||
await insert_slop(domain, path)
|
||||
|
||||
// @ts-ignore
|
||||
update_page_action_icon({frameId: 0, tabId: tab.id, url: tab.url})
|
||||
update_page_action_icon({ frameId: 0, tabId: tab.id, url: tab.url })
|
||||
}
|
||||
|
||||
async function update_page_action_icon(details: browser.webNavigation._OnCommittedDetails) {
|
||||
if(details.frameId != 0) {
|
||||
if (details.frameId != 0) {
|
||||
return
|
||||
}
|
||||
const is_slop = await check_local_slop(details.url)
|
||||
if(is_slop.slop_path) {
|
||||
if (is_slop.slop_path) {
|
||||
browser.pageAction.setIcon({
|
||||
path: "icons/virus_red.png",
|
||||
tabId: details.tabId
|
||||
})
|
||||
}
|
||||
else if(is_slop.slop_domain) {
|
||||
else if (is_slop.slop_domain) {
|
||||
browser.pageAction.setIcon({
|
||||
path: "icons/virus_yellow.png",
|
||||
tabId: details.tabId
|
||||
@@ -191,7 +182,7 @@ async function message_listener(message: any, sender: any) {
|
||||
if (message.type === "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) {
|
||||
@@ -220,32 +211,9 @@ function get_access_token() {
|
||||
return access_token
|
||||
}
|
||||
|
||||
async function submit_login_form() {
|
||||
|
||||
const login_url = new URL("/login", API_URL)
|
||||
|
||||
const request = new Request(login_url,
|
||||
{
|
||||
method: "POST",
|
||||
body: new FormData(login_form)
|
||||
})
|
||||
|
||||
const response = await fetch(request)
|
||||
|
||||
if(response.ok) {
|
||||
const body = await response.json()
|
||||
const token = body.access_token
|
||||
localStorage.setItem("accessToken", token)
|
||||
const status_el = document.getElementById("login-status")
|
||||
status_el.setAttribute("style", "visibility: visible;")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(!login_form) {
|
||||
browser.runtime.onInstalled.addListener(on_install_handler)
|
||||
browser.runtime.onStartup.addListener(get_access_token)
|
||||
browser.pageAction.onClicked.addListener(on_button_clicked_handler)
|
||||
browser.webNavigation.onCommitted.addListener(update_page_action_icon)
|
||||
browser.runtime.onMessage.addListener(message_listener)
|
||||
}
|
||||
browser.runtime.onInstalled.addListener(on_install_handler)
|
||||
browser.runtime.onStartup.addListener(get_access_token)
|
||||
browser.pageAction.onClicked.addListener(on_button_clicked_handler)
|
||||
browser.webNavigation.onCommitted.addListener(update_page_action_icon)
|
||||
browser.runtime.onMessage.addListener(message_listener)
|
||||
|
||||
Reference in New Issue
Block a user