convert to chrome

This commit is contained in:
Gaspard Jankowiak 2026-06-04 22:13:36 +02:00
commit 68a20e8126
3 changed files with 70 additions and 30 deletions

View file

@ -1,16 +1,33 @@
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === "COOKIE") {
return browser.cookies.get({
const extensionApi = globalThis.browser ?? globalThis.chrome;
extensionApi.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type !== "COOKIE") {
sendResponse({ error: `Unknown message type ${message.type}` });
return false;
}
const details = {
url: message.url,
name: message.key
});
// const cookie = await browser.cookies.get({
// url: message.url,
// name: message.key
// });
// console.log(cookie)
// sendResponse({"cookie":cookie});
} else {
throw `Unkown message type ${message.type}`
};
if (globalThis.browser?.cookies?.get) {
extensionApi.cookies.get(details)
.then((cookie) => sendResponse(cookie))
.catch((error) => sendResponse({ error: error.message }));
return true;
}
extensionApi.cookies.get(details, (cookie) => {
const runtimeError = extensionApi.runtime.lastError;
if (runtimeError) {
sendResponse({ error: runtimeError.message });
return;
}
sendResponse(cookie);
});
return true;
});

View file

@ -1,5 +1,7 @@
"use strict";
const extensionApi = globalThis.browser ?? globalThis.chrome;
function getApplicationId() {
const match = window.location.hash.match(/^#\/job-procedures\/record\/([0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12})/)
if (match != null) {
@ -10,16 +12,37 @@ function getApplicationId() {
async function getXSRFToken() {
// we need to go through the background script to get the XSRF-TOKEN cookie
return browser.runtime.sendMessage(
{ type: "COOKIE", url: "https://personal.uni-graz.at", key: "XSRF-TOKEN" }
).then((cookie) => {
return cookie.value;
return new Promise((resolve, reject) => {
extensionApi.runtime.sendMessage(
{ type: "COOKIE", url: "https://personal.uni-graz.at", key: "XSRF-TOKEN" },
(cookie) => {
const runtimeError = extensionApi.runtime.lastError;
if (runtimeError) {
reject(new Error(runtimeError.message))
return
}
if (cookie?.error != null) {
reject(new Error(cookie.error))
return
}
if (cookie?.value == null) {
reject(new Error("Failed to retrieve XSRF token cookie"))
return
}
resolve(cookie.value)
}
)
})
}
async function getApplicant(applicant, token) {
return content.fetch(`https://personal.uni-graz.at/api/erec/job-applications/${applicant.id}`,
return fetch(`https://personal.uni-graz.at/api/erec/job-applications/${applicant.id}`,
{
credentials: "same-origin",
headers: { "X-XSRF-TOKEN": token }
})
.then((response) => {
@ -32,7 +55,7 @@ async function getApplicant(applicant, token) {
const files = jsonData.application_files ?? []
await Promise.all(files.map(async (afile) => {
const response = await content.fetch(
const response = await fetch(
`https://personal.uni-graz.at/api/erec/download-file/${afile.file_id}`,
{
credentials: "same-origin",
@ -54,7 +77,7 @@ async function getApplicant(applicant, token) {
async function getApplicants() {
const aid = getApplicationId()
const token = await getXSRFToken()
return content.fetch(`https://personal.uni-graz.at/api/erec/job-applications/procedure/${aid}`,
return fetch(`https://personal.uni-graz.at/api/erec/job-applications/procedure/${aid}`,
{
credentials: "same-origin",
headers: { "X-XSRF-TOKEN": token }

View file

@ -1,17 +1,17 @@
{
"manifest_version": 2,
"manifest_version": 3,
"name": "KF-Application-Downloader",
"version": "0.1.0",
"browser_specific_settings": {
"gecko": {
"id": "gaspard.jankowiak@uni-graz.at"
}
},
"description": "Downloads all files from an application procedure at the KFU",
"permissions": [
"cookies"
],
"host_permissions": [
"https://personal.uni-graz.at/*"
],
"background": {
"scripts": ["background.js"]
"service_worker": "background.js"
},
"permissions": ["*://personal.uni-graz.at/*", "cookies"],
"description": "Downloads all files from a application procedure at the KFU",
"content_scripts": [
{
"matches": [