tweaking for alpha

This commit is contained in:
Jack Case
2025-10-25 11:27:20 -04:00
parent 0cf4d5253e
commit 0e00bb8b1e
4 changed files with 71 additions and 16 deletions

View File

@@ -25,7 +25,7 @@
},
"background": {
"page": "pages/background_page.html"
"scripts": ["scripts/report-slop.js"]
},
"content_scripts": [

View File

@@ -3,15 +3,17 @@
<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>
<title>Slop Farmer</title>
</head>
<body>
<h1>Hello, world!</h1>
<form action="" method="post">
<label for="email">email:</label>
<input type="email" name="username" required />
<label for="password">password:</label>
<form id="login-form">
<label for="email" id="username">user:</label>
<input type="text" name="username" required />
<label for="password" id="password">password:</label>
<input type="password" name="password" required />
<input type="submit" value="login" />
<button id="login-button">login</button>
</form>
</body>

View File

@@ -65,9 +65,24 @@ function setup_result_observer() {
result_list_observer.observe(result_list_node, config)
}
function onload_handler() {
async function wait_for_results() {
results = new Promise(async (resolve) => {
let node = document.querySelector(ddg_result_list_selector)
while (!node) {
await new Promise((resolve) => {setTimeout(()=>{resolve()}, 100)})
node = document.querySelector(ddg_result_list_selector)
}
resolve(node)
})
return results
}
async function onload_handler() {
// get results ol node to observe
result_list_node = document.querySelector(ddg_result_list_selector)
result_list_node = await wait_for_results()
get_initial_links()
setup_result_observer()
@@ -80,5 +95,5 @@ browser.runtime.onMessage.addListener(message_listener)
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", onload_handler)
} else {
onload_handler()
wait_for_results().then(onload_handler)
}

View File

@@ -1,6 +1,11 @@
const API_URL = "http://localhost:8000"
let access_token
const login_form = document.getElementById("login-form")
if(login_form) {
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",
@@ -15,14 +20,17 @@ function setup_storage_db() {
db_request.onerror = (event) => {
// handle error
console.log(event)
}
db_request.onsuccess = (event) => {
// create objectstore
console.log(event)
db = event.target.result
}
db_request.onupgradeneeded = (event) => {
console.log(event)
db = event.target.result
const slop_store = db.createObjectStore("slop", {keyPath: "domain"})
}
@@ -85,7 +93,15 @@ async function insert_slop(domain, path) {
}
const report_url = new URL("/report", API_URL)
const request = new Request(report_url, {method: "POST", headers: {"Content-Type": "application/json"}, body: JSON.stringify({slop_urls: [new URL(path, "http://"+domain).toString()]})})
const request = new Request(report_url,
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Bearer": get_access_token()
},
body: JSON.stringify({slop_urls: [new URL(path, "http://"+domain).toString()]})
})
fetch(request)
}
@@ -118,7 +134,7 @@ async function check_local_slop(url) {
async function check_remote_slop(urls) {
const check_url = new URL("/check", API_URL)
const request = new Request(check_url, {method: "POST", headers: { "Content-Type": "application/json" }, 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) => {insert_slop(domain.domain_name, "/")})
@@ -195,8 +211,30 @@ 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)
}
}
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)
}