From 68a20e812607d683f41b2af1f2f7b128b7853cba Mon Sep 17 00:00:00 2001 From: Gaspard Jankowiak Date: Thu, 4 Jun 2026 22:13:36 +0200 Subject: [PATCH] convert to chrome --- background.js | 45 +++++++++++++++++++++++++++++++-------------- content.js | 37 ++++++++++++++++++++++++++++++------- manifest.json | 18 +++++++++--------- 3 files changed, 70 insertions(+), 30 deletions(-) diff --git a/background.js b/background.js index e350bd6..6696cd0 100644 --- a/background.js +++ b/background.js @@ -1,16 +1,33 @@ -browser.runtime.onMessage.addListener((message, sender, sendResponse) => { - if (message.type === "COOKIE") { - return browser.cookies.get({ - 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}` +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 + }; + + 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; }); diff --git a/content.js b/content.js index 8eeb558..aab33ef 100644 --- a/content.js +++ b/content.js @@ -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 } diff --git a/manifest.json b/manifest.json index f3a4df7..dd7c5eb 100644 --- a/manifest.json +++ b/manifest.json @@ -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": [