From ea33455964e231365112ddf61eff445638c40dc3 Mon Sep 17 00:00:00 2001 From: msramalho <19508417+msramalho@users.noreply.github.com> Date: Fri, 24 Feb 2023 15:41:58 +0100 Subject: [PATCH] oauth --- source/html/popup.html | 2 +- source/js/background.js | 70 +++++++++++++++++++++++++++++------------ source/manifest.json | 6 +++- source/vue/Popup.vue | 11 ++++++- 4 files changed, 66 insertions(+), 23 deletions(-) diff --git a/source/html/popup.html b/source/html/popup.html index f6742a8..7ba1823 100644 --- a/source/html/popup.html +++ b/source/html/popup.html @@ -10,7 +10,7 @@ -
+
diff --git a/source/js/background.js b/source/js/background.js index f441ac3..0a03ed6 100644 --- a/source/js/background.js +++ b/source/js/background.js @@ -2,6 +2,8 @@ // import './options-storage.js'; import optionsStorage from './options-storage.js'; +// TODO: stable ID https://developer.chrome.com/docs/extensions/mv3/tut_oauth/ +// TODO: API_ENDPOINT depending on deployment const API_ENDPOINT = 'http://localhost:8000/tasks' chrome.runtime.onMessage.addListener(((r, s, sR) => { @@ -11,42 +13,50 @@ chrome.runtime.onMessage.addListener(((r, s, sR) => { async function processMessages(request, sender, sendResponse) { console.info(`action {${request.action}} from ${sender.tab ? 'content-script (' + sender.tab.url + ')' : 'the extension'}`) - if (request.action === "archive") { - archiveUrl(sendResponse); - } else if (request.action === "status") { - const task_db = await getTaskById(request.task.task_id); - if (task_db?.status == "SUCCESS" || task_db?.status == 'FAILURE') { - console.log("ALREADY FINSIHED, NO REQS") - sendResponse(task_db) + chrome.identity.getAuthToken({ interactive: true }, async function (access_token) { + console.log(access_token); + if (request.action === "archive") { + archiveUrl(sendResponse, access_token); + } else if (request.action === "search") { + const tasks = await search(request.query, access_token); + sendResponse(tasks); + } else if (request.action === "status") { + const task_db = await getTaskById(request.task.task_id); + if (task_db?.status == "SUCCESS" || task_db?.status == 'FAILURE') { + console.log("ALREADY FINSIHED, NO REQS") + sendResponse(task_db) + } + const task_fresh = await checkTaskStatus(request.task, access_token) + sendResponse(task_fresh) + } else if (request.action === "getTasks") { + sendResponse(await getAllTasks()); } - const task_fresh = await checkTaskStatus(request.task) - sendResponse(task_fresh) - } else if (request.action === "getTasks") { - sendResponse(await getAllTasks()); - } + }); } -function archiveUrl(sendResponse) { +function archiveUrl(sendResponse, access_token) { chrome.tabs.query({ active: true, lastFocusedWindow: true }, async (tabs) => { let url = tabs[0].url; console.log(`url=${url}`); - const response = await submitUrlTask(url) + const response = await searchTask(url, access_token); const new_archive = { url, task_id: response.task_id, status: 'PENDING', result: {} }; await upsertTask(new_archive); sendResponse(new_archive); }); } -function submitUrlTask(url) { +function searchTask(url, access_token) { console.log(`API: SUBMIT`) return new Promise((resolve, reject) => { fetch(API_ENDPOINT, { method: 'POST', - headers: { 'Content-Type': 'application/json' },//, 'X-API-KEY': 'TODO' }, - body: JSON.stringify({ url }), + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ url, access_token }), }).then( response => response.json(), ).then(response => resolve(response) @@ -57,12 +67,14 @@ function submitUrlTask(url) { }) } -function checkTaskStatus(task) { +function checkTaskStatus(task, access_token) { console.log(`API: STATUS`) return new Promise((resolve, reject) => { - fetch(`${API_ENDPOINT}/${task.task_id}`, { + fetch(`${API_ENDPOINT}/${task.task_id}?` + new URLSearchParams({ access_token }), { method: 'GET', - headers: { 'Content-Type': 'application/json' }, + headers: { + 'Content-Type': 'application/json', + }, }).then( response => response.json(), ).then(response => { @@ -80,6 +92,24 @@ function checkTaskStatus(task) { }) } +function search(query, access_token) { + console.log(`API: SEARCH`) + return new Promise((resolve, reject) => { + fetch(`${API_ENDPOINT}/search?` + new URLSearchParams({ access_token, query }), { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + }, + }).then( + response => response.json(), + ).then(response => resolve(response) + ).catch(err => { + console.log(`There was an error: ${err}`) + reject(err) + }); + }) +} + async function getAllTasks() { const storage = await optionsStorage.getAll(); return storage.archived_urls; diff --git a/source/manifest.json b/source/manifest.json index 9281c48..c3a6225 100644 --- a/source/manifest.json +++ b/source/manifest.json @@ -15,7 +15,7 @@ "128": "img/icon.png" }, "permissions": [ - "storage", "tabs" + "storage", "tabs", "identity" ], "host_permissions": [ "*://*/*" @@ -31,5 +31,9 @@ "options_ui": { "browser_style": true, "page": "html/options.html" + }, + "oauth2": { + "client_id": "572076445849-4cb2a8be1nfi46l80jm741k56s7cjkd0.apps.googleusercontent.com", + "scopes": ["https://www.googleapis.com/auth/userinfo.email"] } } \ No newline at end of file diff --git a/source/vue/Popup.vue b/source/vue/Popup.vue index 8e6e86d..201e6cb 100644 --- a/source/vue/Popup.vue +++ b/source/vue/Popup.vue @@ -3,6 +3,7 @@ icon Auto Archiver extension +
search @@ -47,12 +48,20 @@ export default { const response = await chrome.runtime.sendMessage({ action: "archive" }); - // do something with response here, not outside the function this.url = response.url; this.task_id = response.task_id; this.addTask(response) })(); }, + searchF: function(){ + (async () => { + const response = await chrome.runtime.sendMessage({ + action: "search", + query: "search query" + }); + console.log(response) + })(); + }, displayAllTasks: function () { (async () => { const tasks = await chrome.runtime.sendMessage({