From ef74fa3b5fce13312b476b58ec86784465f14584 Mon Sep 17 00:00:00 2001 From: Wendelin Keimel Date: Thu, 27 Jan 2022 11:12:54 +0100 Subject: [PATCH] toggl-task anlegen --- content_script.js | 138 +++++++++++++++++++++++++++++++++------------- manifest.json | 5 +- 2 files changed, 104 insertions(+), 39 deletions(-) diff --git a/content_script.js b/content_script.js index 03bddd0..87d6381 100644 --- a/content_script.js +++ b/content_script.js @@ -19,55 +19,119 @@ function addButton() { } const mappings = { + "Globalgold": "gg", "GG": "gg", "Audience Builder": "ab", - "Audience Explorer": "ae", + "Audience Exporter": "ae", "Cta Calls": "xcta", "Camper": "camper", + "Praktikum": "pr" }; function onClick() { - let apiKey = "afadffe77f745496f80ebb4bf460c615" + 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 = "***" - console.log("Hallo") - const longId = window.location.pathname.split("/")[2]; - var url = `https://api.trello.com/1/cards/${longId}?key=${apiKey}&token=${token}` - fetch(url).then(response => response.json()).then(response => { - shortId = response["idShort"] - - if (!response["name"].endsWith(shortId)) { - for (var i in response["labels"]) { - if (labelShort == undefined) { - var labelShort = mappings[response["labels"][i]["name"]] - } else if (mappings[response["labels"][i]["name"]] != undefined) { - alert(`Diese Karte hat sowohl "${labelShort}" als auch "${mappings[response["labels"][i]["name"]]}".`) - } - } + 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"] - if (labelShort != undefined) { - - - const url = new URL(`https://api.trello.com/1/cards/${longId}`) - console.log(url) - - const data = { - "name": `${response["name"]} #${labelShort}_${shortId}` - }; - const jsonBody = JSON.stringify(data); - fetch(url.toString(), { - method: 'PUT', - headers: { - 'Authorization': `OAuth oauth_consumer_key="${apiKey}", oauth_token="${token}"`, - 'Content-Type': 'application/json' - }, - body: jsonBody - }).then(response => { - console.log(response) - }) + 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 + } + + }) + + + } + + url = new URL("https://api.track.toggl.com/api/v8/workspaces") + const togglAuth = btoa(`${togglToken}:api_token`) + 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"] == "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) + }).then(response => { + console.log(response) + }) + } + } + }) + }) + }) } diff --git a/manifest.json b/manifest.json index 6c61666..ac60555 100644 --- a/manifest.json +++ b/manifest.json @@ -19,10 +19,11 @@ ], "permissions": [ - "*://api.trello.com/*" + "*://api.trello.com/*", + "*://*.toggl.com/*" ], "options_ui": { "page": "options/index.html" } -} \ No newline at end of file +}