convert to chrome
This commit is contained in:
parent
f5c23dc867
commit
68a20e8126
3 changed files with 70 additions and 30 deletions
|
|
@ -1,16 +1,33 @@
|
||||||
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
const extensionApi = globalThis.browser ?? globalThis.chrome;
|
||||||
if (message.type === "COOKIE") {
|
|
||||||
return browser.cookies.get({
|
extensionApi.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
||||||
url: message.url,
|
if (message.type !== "COOKIE") {
|
||||||
name: message.key
|
sendResponse({ error: `Unknown message type ${message.type}` });
|
||||||
});
|
return false;
|
||||||
// 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 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;
|
||||||
});
|
});
|
||||||
|
|
|
||||||
37
content.js
37
content.js
|
|
@ -1,5 +1,7 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
const extensionApi = globalThis.browser ?? globalThis.chrome;
|
||||||
|
|
||||||
function getApplicationId() {
|
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})/)
|
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) {
|
if (match != null) {
|
||||||
|
|
@ -10,16 +12,37 @@ function getApplicationId() {
|
||||||
|
|
||||||
async function getXSRFToken() {
|
async function getXSRFToken() {
|
||||||
// we need to go through the background script to get the XSRF-TOKEN cookie
|
// we need to go through the background script to get the XSRF-TOKEN cookie
|
||||||
return browser.runtime.sendMessage(
|
return new Promise((resolve, reject) => {
|
||||||
{ type: "COOKIE", url: "https://personal.uni-graz.at", key: "XSRF-TOKEN" }
|
extensionApi.runtime.sendMessage(
|
||||||
).then((cookie) => {
|
{ type: "COOKIE", url: "https://personal.uni-graz.at", key: "XSRF-TOKEN" },
|
||||||
return cookie.value;
|
(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) {
|
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 }
|
headers: { "X-XSRF-TOKEN": token }
|
||||||
})
|
})
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
|
|
@ -32,7 +55,7 @@ async function getApplicant(applicant, token) {
|
||||||
const files = jsonData.application_files ?? []
|
const files = jsonData.application_files ?? []
|
||||||
|
|
||||||
await Promise.all(files.map(async (afile) => {
|
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}`,
|
`https://personal.uni-graz.at/api/erec/download-file/${afile.file_id}`,
|
||||||
{
|
{
|
||||||
credentials: "same-origin",
|
credentials: "same-origin",
|
||||||
|
|
@ -54,7 +77,7 @@ async function getApplicant(applicant, token) {
|
||||||
async function getApplicants() {
|
async function getApplicants() {
|
||||||
const aid = getApplicationId()
|
const aid = getApplicationId()
|
||||||
const token = await getXSRFToken()
|
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",
|
credentials: "same-origin",
|
||||||
headers: { "X-XSRF-TOKEN": token }
|
headers: { "X-XSRF-TOKEN": token }
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
{
|
{
|
||||||
"manifest_version": 2,
|
"manifest_version": 3,
|
||||||
"name": "KF-Application-Downloader",
|
"name": "KF-Application-Downloader",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"browser_specific_settings": {
|
"description": "Downloads all files from an application procedure at the KFU",
|
||||||
"gecko": {
|
"permissions": [
|
||||||
"id": "gaspard.jankowiak@uni-graz.at"
|
"cookies"
|
||||||
}
|
],
|
||||||
},
|
"host_permissions": [
|
||||||
|
"https://personal.uni-graz.at/*"
|
||||||
|
],
|
||||||
"background": {
|
"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": [
|
"content_scripts": [
|
||||||
{
|
{
|
||||||
"matches": [
|
"matches": [
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue