diff --git a/background.js b/background.js new file mode 100644 index 0000000..b8a73f5 --- /dev/null +++ b/background.js @@ -0,0 +1,12 @@ +trektor.runtime.onMessage((msg) => { + switch (msg.action) { + case 'fetchJSON': + return fetchJSON(...msg.args); + default: + return Promise.reject('unknown action'); + } +}); + +function fetchJSON(url, options = {}) { + return fetch(url, options).then((response) => response.json()); +} diff --git a/content_script.js b/content_script.js index 83a76cc..dbde5ff 100644 --- a/content_script.js +++ b/content_script.js @@ -1,11 +1,3 @@ -let engine; - -if (window.browser !== undefined) { - engine = browser; -} else { - engine = chrome; -} - function addButton() { const sidebar = document.querySelector(".window-sidebar") const button = document.createElement('span'); @@ -37,7 +29,7 @@ const mappings = { function onClick() { let trelloApiKey = "afadffe77f745496f80ebb4bf460c615" - engine.storage.local.get().then(result => { + trektor.storage.get(['trello', 'toggl']).then(result => { const trelloToken = result.trello const togglToken = result.toggl @@ -94,12 +86,12 @@ function onClick() { url = new URL("https://api.track.toggl.com/api/v8/workspaces") const togglAuth = btoa(`${togglToken}:api_token`) - fetch(url.toString(), { + trektor.fetchJSON(url.toString(), { headers: { 'Authorization': `Basic ${togglAuth}`, 'Content-Type': 'application/json' } - }).then(response => response.json()).then(response => { + }).then(response => { for (var i in response) { if (response[i]["name"] == "aboutsource") { var wid = response[i]["id"] @@ -111,12 +103,12 @@ function onClick() { } url = new URL(`https://api.track.toggl.com/api/v8/workspaces/${wid}/projects`) - fetch(url.toString(), { + trektor.fetchJSON(url.toString(), { headers: { 'Authorization': `Basic ${togglAuth}`, 'Content-Type': 'application/json' } - }).then(response => response.json()).then(response => { + }).then(response => { for (var i in response) { if (response[i]["name"].endsWith(`(${labelShort})`)) { var pid = response[i]["id"] @@ -128,7 +120,7 @@ function onClick() { "pid": pid } } - fetch(url.toString(), { + trektor.fetchJSON(url.toString(), { method: 'POST', headers: { 'Authorization': `Basic ${togglAuth}`, diff --git a/manifest.json b/manifest.json index 6d9c6ed..939d602 100644 --- a/manifest.json +++ b/manifest.json @@ -13,11 +13,17 @@ "*://trello.com/*" ], "js": [ + "trektor.js", "content_script.js" ] } ], - + "background": { + "scripts": [ + "trektor.js", + "background.js" + ] + }, "permissions": [ "*://api.trello.com/*", "*://*.toggl.com/*", diff --git a/options/index.html b/options/index.html index f21cce0..743838d 100644 --- a/options/index.html +++ b/options/index.html @@ -11,8 +11,9 @@