diff --git a/content_script.js b/content_script.js index 87d6381..83a76cc 100644 --- a/content_script.js +++ b/content_script.js @@ -1,3 +1,11 @@ +let engine; + +if (window.browser !== undefined) { + engine = browser; +} else { + engine = chrome; +} + function addButton() { const sidebar = document.querySelector(".window-sidebar") const button = document.createElement('span'); @@ -19,7 +27,6 @@ function addButton() { } const mappings = { - "Globalgold": "gg", "GG": "gg", "Audience Builder": "ab", "Audience Exporter": "ae", @@ -29,77 +36,64 @@ const mappings = { }; function onClick() { - let apiKey = "***" - //generate token at https://trello.com/1/authorize?expiration=never&scope=read,write,account&response_type=token&name=Server%20Token&key=afadffe77f745496f80ebb4bf460c615 - let token = "af00d9597af9fb897f6b2fedb3b7152845991fe186caca08cf5dbb03bba40ffc" - let togglToken = "***" - - const idLong = window.location.pathname.split("/")[2]; - var url = new URL(`https://api.trello.com/1/cards/${idLong}?key=${apiKey}&token=${token}`) - fetch(url.toString()).then(response => response.json()).then(response => { - const idShort = response["idShort"] - - console.log("Hallo") - var labelShort, labelLong = undefined - for (var i in response["labels"]) { - if (labelShort == undefined) { - labelShort = mappings[response["labels"][i]["name"]] - labelLong = (labelShort != undefined) ? response["labels"][i]["name"] : labelLong - } else if (mappings[response["labels"][i]["name"]] != undefined) { - alert(`Diese Karte hat sowohl "${labelShort}" als auch "${mappings[response["labels"][i]["name"]]}".`) - } - } - - if (labelShort == undefined) { - alert("Diese Karte besitzt kein in den mappings vorhandenes Label.") - return false - } - - if (!response["name"].endsWith(`#${labelShort}_${idShort}`)) { - - - url = new URL(`https://api.trello.com/1/cards/${idLong}`) - - var data = { - "name": `${response["name"]} #${labelShort}_${idShort}` - }; - fetch(url.toString(), { - method: 'PUT', - headers: { - 'Authorization': `OAuth oauth_consumer_key="${apiKey}", oauth_token="${token}"`, - 'Content-Type': 'application/json' - }, - body: JSON.stringify(data) - }).then(response => { - if (!response.ok) { - alert(`Error ${response.status}:\n${response.statusText}`) - return false - } + let trelloApiKey = "afadffe77f745496f80ebb4bf460c615" + engine.storage.local.get().then(result => { + const trelloToken = result.trello + const togglToken = result.toggl - }) - - - } - - url = new URL("https://api.track.toggl.com/api/v8/workspaces") - const togglAuth = btoa(`${togglToken}:api_token`) + const idLong = window.location.pathname.split("/")[2]; + var url = new URL(`https://api.trello.com/1/cards/${idLong}`) fetch(url.toString(), { headers: { - 'Authorization': `Basic ${togglAuth}`, + 'Authorization': `OAuth oauth_consumer_key="${trelloApiKey}", oauth_token="${trelloToken}"`, 'Content-Type': 'application/json' - } + }, }).then(response => response.json()).then(response => { - for (var i in response) { - if (response[i]["name"] == "aboutsource") { - var wid = response[i]["id"] + const idShort = response["idShort"] + + var labelShort, labelLong = undefined + for (var i in response["labels"]) { + if (labelShort == undefined) { + labelShort = mappings[response["labels"][i]["name"]] + labelLong = (labelShort != undefined) ? response["labels"][i]["name"] : labelLong + } else if (mappings[response["labels"][i]["name"]] != undefined) { + alert(`Diese Karte hat sowohl "${labelShort}" als auch "${mappings[response["labels"][i]["name"]]}".`) } } - if (wid == undefined) { - alert("WorkspaceID undefined\nDies bedeutet, du hast entweder keinen Zugriff zum a:s toggl oder es ist kein API-Token angegeben.") + + if (labelShort == undefined) { + alert("Diese Karte besitzt kein unterstütztes Projekt Label.") return false } - url = new URL(`https://api.track.toggl.com/api/v8/workspaces/${wid}/projects`) + if (!response["name"].endsWith(`#${labelShort}_${idShort}`)) { + + + url = new URL(`https://api.trello.com/1/cards/${idLong}`) + + var data = { + "name": `${response["name"]} #${labelShort}_${idShort}` + }; + fetch(url.toString(), { + method: 'PUT', + headers: { + 'Authorization': `OAuth oauth_consumer_key="${trelloApiKey}", oauth_token="${trelloToken}"`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify(data) + }).then(response => { + if (!response.ok) { + alert(`Error ${response.status}:\n${response.statusText}`) + return false + } + + }) + + + } + + url = new URL("https://api.track.toggl.com/api/v8/workspaces") + const togglAuth = btoa(`${togglToken}:api_token`) fetch(url.toString(), { headers: { 'Authorization': `Basic ${togglAuth}`, @@ -107,32 +101,50 @@ function onClick() { } }).then(response => response.json()).then(response => { for (var i in response) { - if (response[i]["name"].endsWith(`(${labelShort})`)) { - var pid = response[i]["id"] - - url = new URL('https://api.track.toggl.com/api/v8/tasks') - data = { - "task": { - "name": `${labelShort}_${idShort}`, - "pid": pid + if (response[i]["name"] == "aboutsource") { + var wid = response[i]["id"] + } + } + if (wid == undefined) { + alert("WorkspaceID undefined\nDies bedeutet, du hast entweder keinen Zugriff zum a:s toggl oder es ist kein API-Token angegeben.") + return false + } + + url = new URL(`https://api.track.toggl.com/api/v8/workspaces/${wid}/projects`) + fetch(url.toString(), { + headers: { + 'Authorization': `Basic ${togglAuth}`, + 'Content-Type': 'application/json' + } + }).then(response => response.json()).then(response => { + for (var i in response) { + if (response[i]["name"].endsWith(`(${labelShort})`)) { + var pid = response[i]["id"] + + url = new URL('https://api.track.toggl.com/api/v8/tasks') + data = { + "task": { + "name": `${labelShort}_${idShort}`, + "pid": pid + } } + fetch(url.toString(), { + method: 'POST', + headers: { + 'Authorization': `Basic ${togglAuth}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify(data) + }) } - fetch(url.toString(), { - method: 'POST', - headers: { - 'Authorization': `Basic ${togglAuth}`, - 'Content-Type': 'application/json' - }, - body: JSON.stringify(data) - }).then(response => { - console.log(response) - }) } - } + }) }) - }) + }) }) + + } window.addEventListener("pushstate", function () { diff --git a/icons/64.png b/icons/64.png new file mode 100644 index 0000000..1da020e Binary files /dev/null and b/icons/64.png differ diff --git a/icons/icon.png b/icons/icon.png deleted file mode 100644 index 063cfa8..0000000 Binary files a/icons/icon.png and /dev/null differ diff --git a/manifest.json b/manifest.json index ac60555..6d9c6ed 100644 --- a/manifest.json +++ b/manifest.json @@ -4,7 +4,7 @@ "description": "Browser-Extension zum automatischen Anlegen von Toggl tracking tasks", "version": "0.0.1", "icons": { - "64": "icons/icon.png" + "64": "icons/64.png" }, "content_scripts": [ @@ -20,7 +20,8 @@ "permissions": [ "*://api.trello.com/*", - "*://*.toggl.com/*" + "*://*.toggl.com/*", + "storage" ], "options_ui": { diff --git a/options/index.html b/options/index.html index ed2c975..f21cce0 100644 --- a/options/index.html +++ b/options/index.html @@ -1,11 +1,18 @@ + + Trektor settings - -

Extension settings

+ +

Trektor settings

+ + + Trello Token generieren
+
Toggl Token (ziemlich weit runter scrollen)
+


diff --git a/options/script.js b/options/script.js index 5a71dfb..065c504 100644 --- a/options/script.js +++ b/options/script.js @@ -1 +1,27 @@ -//extension settings script +let engine; + +if (window.browser !== undefined) { + engine = browser; +} else { + engine = chrome; +} + +const trelloTextField = document.querySelector("#trello_token") +const togglTextField = document.querySelector("#toggl_token") + +trelloTextField.addEventListener("input", onChange) +togglTextField.addEventListener("input", onChange) + +function onChange() { + engine.storage.local.set({ + "trello": trelloTextField.value, + "toggl": togglTextField.value + }) + console.log(`set trello token to ${togglTextField.value}`) + console.log(`set toggl token to ${togglTextField.value}`) +} + +engine.storage.local.get().then(result => { + trelloTextField.value = result["trello"] + togglTextField.value = result["toggl"] +})