From add11da14cd372198e36da8caad88a8554fc58f1 Mon Sep 17 00:00:00 2001 From: Wendelin Keimel Date: Tue, 25 Jan 2022 14:12:07 +0100 Subject: [PATCH 1/8] add api request permission --- manifest.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/manifest.json b/manifest.json index d6f93dc..5fe4a50 100644 --- a/manifest.json +++ b/manifest.json @@ -18,6 +18,10 @@ } ], + "permissions": [ + "*://api.trello.com/*" + ], + "options_ui": { "page": "options/index.html" } From d10d3c07be6defa72dabe77168642c6d3dc5c649 Mon Sep 17 00:00:00 2001 From: Wendelin Keimel Date: Tue, 25 Jan 2022 14:16:30 +0100 Subject: [PATCH 2/8] get board id and number --- content_script.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/content_script.js b/content_script.js index 728fa4d..57f74fe 100644 --- a/content_script.js +++ b/content_script.js @@ -1,16 +1,25 @@ -function addButton(){ +function addButton() { const sidebar = document.querySelector(".window-sidebar") sidebar.innerHTML = "+Toggl Task" + sidebar.innerHTML - sidebar.querySelectorAll(".mod-no-top-margin")[1].classList.remove("mod-no-top-margin") + sidebar.querySelector(".js-sidebar-add-heading").classList.remove("mod-no-top-margin") document.querySelector("#togglbtn").addEventListener("click", onClick) } -function onClick(){ - alert("work in progress ;)") +function onClick() { + var apiKey = "afadffe77f745496f80ebb4bf460c615" + //generate token at https://trello.com/1/authorize?expiration=never&scope=read,write,account&response_type=token&name=Server%20Token&key=afadffe77f745496f80ebb4bf460c615 + var token = "af00d9597af9fb897f6b2fedb3b7152845991fe186caca08cf5dbb03bba40ffc" + + var id = window.location.href.split("/")[4] + var url = 'https://api.trello.com/1/cards/' + id + '?key=' + apiKey + '&token=' + token + console.log(url) + fetch(url).then(response => response.json()).then(response => { + console.log(response["url"].split("/")[5].split("-")[0]) + }) } -window.addEventListener("pushstate", function(){ - if(window.location.pathname.startsWith("/c/")){ +window.addEventListener("pushstate", function () { + if (window.location.pathname.startsWith("/c/")) { addButton() } }) \ No newline at end of file From a31c80ef1ad514f08ad1d75a95e7edfdd7a5a392 Mon Sep 17 00:00:00 2001 From: Wendelin Keimel Date: Tue, 25 Jan 2022 17:12:12 +0100 Subject: [PATCH 3/8] =?UTF-8?q?tracking=20task=20an=20Titel=20anh=C3=A4nge?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- content_script.js | 47 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/content_script.js b/content_script.js index 57f74fe..7d35155 100644 --- a/content_script.js +++ b/content_script.js @@ -1,20 +1,51 @@ function addButton() { - const sidebar = document.querySelector(".window-sidebar") + let sidebar = document.querySelector(".window-sidebar") sidebar.innerHTML = "+Toggl Task" + sidebar.innerHTML + sidebar.querySelector(".mod-no-top-margin").classList.remove("mod-no-top-margin") sidebar.querySelector(".js-sidebar-add-heading").classList.remove("mod-no-top-margin") document.querySelector("#togglbtn").addEventListener("click", onClick) } +var mappings = [] +mappings["GG"] = "gg" +mappings["Audience Builder"] = "ab" +mappings["Audience Explorer"] = "ae" +mappings["Cta Calls"] = "xcta" +mappings["Camper"] = "camper" + function onClick() { - var apiKey = "afadffe77f745496f80ebb4bf460c615" + let apiKey = "afadffe77f745496f80ebb4bf460c615" //generate token at https://trello.com/1/authorize?expiration=never&scope=read,write,account&response_type=token&name=Server%20Token&key=afadffe77f745496f80ebb4bf460c615 - var token = "af00d9597af9fb897f6b2fedb3b7152845991fe186caca08cf5dbb03bba40ffc" - - var id = window.location.href.split("/")[4] - var url = 'https://api.trello.com/1/cards/' + id + '?key=' + apiKey + '&token=' + token - console.log(url) + let token = "af00d9597af9fb897f6b2fedb3b7152845991fe186caca08cf5dbb03bba40ffc" + console.log("Hallo") + let longId = window.location.href.split("/")[4] + var url = 'https://api.trello.com/1/cards/' + longId + '?key=' + apiKey + '&token=' + token fetch(url).then(response => response.json()).then(response => { - console.log(response["url"].split("/")[5].split("-")[0]) + shortId = response["idShort"] + + if (!response["name"].endsWith(shortId)) { + for (var i in response["labels"]) { + var labelShort = mappings[response["labels"][i]["name"]] + console.log(labelShort) + } + + if (labelShort != undefined) { + const fixedEncodeURIComponent = (str) => { + return encodeURIComponent(str).replace(/[!'()*]/g, (c) => { + return '%' + c.charCodeAt(0).toString(16); + }); + } + var url = 'https://api.trello.com/1/cards/' + longId + '?name=' + fixedEncodeURIComponent(response["name"]) + fixedEncodeURIComponent("#") + labelShort + "_" + shortId + '&key=' + apiKey + '&token=' + token + console.log(url) + fetch(url, { + method: 'PUT' + }).then(response => {//response.json()).then(response => { + console.log(response) + console.log("HURRA!") + }) + } + } + }) } From e27f2950f74867690de3bbe7a1772c859bda2afd Mon Sep 17 00:00:00 2001 From: Wendelin Keimel Date: Tue, 25 Jan 2022 17:21:03 +0100 Subject: [PATCH 4/8] add space between title and # --- content_script.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content_script.js b/content_script.js index f76c299..7d17bd6 100644 --- a/content_script.js +++ b/content_script.js @@ -28,7 +28,7 @@ mappings["Camper"] = "camper" function onClick() { let apiKey = "afadffe77f745496f80ebb4bf460c615" //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 token = "***" console.log("Hallo") let longId = window.location.href.split("/")[4] var url = 'https://api.trello.com/1/cards/' + longId + '?key=' + apiKey + '&token=' + token @@ -47,7 +47,7 @@ function onClick() { return '%' + c.charCodeAt(0).toString(16); }); } - var url = 'https://api.trello.com/1/cards/' + longId + '?name=' + fixedEncodeURIComponent(response["name"]) + fixedEncodeURIComponent("#") + labelShort + "_" + shortId + '&key=' + apiKey + '&token=' + token + var url = 'https://api.trello.com/1/cards/' + longId + '?name=' + fixedEncodeURIComponent(response["name"]) + fixedEncodeURIComponent(" #") + labelShort + "_" + shortId + '&key=' + apiKey + '&token=' + token console.log(url) fetch(url, { method: 'PUT' From 7bc675084818e92d68d591f5e03d2f6066d9142b Mon Sep 17 00:00:00 2001 From: WendelinPraktikum <98315207+WendelinPraktikum@users.noreply.github.com> Date: Wed, 26 Jan 2022 09:57:46 +0100 Subject: [PATCH 5/8] Update content_script.js Co-authored-by: Martin Kalcher --- content_script.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/content_script.js b/content_script.js index 7d17bd6..b94120f 100644 --- a/content_script.js +++ b/content_script.js @@ -18,12 +18,13 @@ function addButton(){ sidebar.querySelector(".js-sidebar-add-heading").classList.remove("mod-no-top-margin") } -var mappings = [] -mappings["GG"] = "gg" -mappings["Audience Builder"] = "ab" -mappings["Audience Explorer"] = "ae" -mappings["Cta Calls"] = "xcta" -mappings["Camper"] = "camper" +const mappings = { + "GG": "gg", + "Audience Builder": "ab", + "Audience Explorer": "ae", + "Cta Calls": "xcta", + "Camper": "camper", +}; function onClick() { let apiKey = "afadffe77f745496f80ebb4bf460c615" From feb47390a8484130d6b9142ca3535521a72c3fda Mon Sep 17 00:00:00 2001 From: WendelinPraktikum <98315207+WendelinPraktikum@users.noreply.github.com> Date: Wed, 26 Jan 2022 09:58:44 +0100 Subject: [PATCH 6/8] use window.location.pathname instead of window.location.href Co-authored-by: Martin Kalcher --- content_script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content_script.js b/content_script.js index b94120f..9cd781b 100644 --- a/content_script.js +++ b/content_script.js @@ -31,7 +31,7 @@ function onClick() { //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") - let longId = window.location.href.split("/")[4] + 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"] From e4a120214ef68fc133190b2475ac5c7838689e8e Mon Sep 17 00:00:00 2001 From: WendelinPraktikum <98315207+WendelinPraktikum@users.noreply.github.com> Date: Wed, 26 Jan 2022 10:04:08 +0100 Subject: [PATCH 7/8] use const instead of let for button and sidebar --- content_script.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/content_script.js b/content_script.js index 9cd781b..b1e2da2 100644 --- a/content_script.js +++ b/content_script.js @@ -1,16 +1,16 @@ function addButton(){ - let sidebar = document.querySelector(".window-sidebar") - let button = document.createElement('span'); + const sidebar = document.querySelector(".window-sidebar") + const button = document.createElement('span'); button.classList.add('button-link'); button.addEventListener('click', onClick); sidebar.prepend(button); - let buttonIcon = document.createElement('span'); + const buttonIcon = document.createElement('span'); buttonIcon.classList.add('icon-sm', 'plugin-icon'); buttonIcon.innerText = '+'; button.append(buttonIcon); - let buttonText = document.createElement('span'); + const buttonText = document.createElement('span'); buttonText.innerText = 'Toggl Task'; button.append(buttonText); From eacb62f9b2e66472cb032d939594bcaf777dc9ff Mon Sep 17 00:00:00 2001 From: Wendelin Keimel Date: Wed, 26 Jan 2022 14:42:01 +0100 Subject: [PATCH 8/8] use string interpolation and url api, put token in header --- content_script.js | 49 ++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/content_script.js b/content_script.js index b1e2da2..03bddd0 100644 --- a/content_script.js +++ b/content_script.js @@ -1,4 +1,4 @@ -function addButton(){ +function addButton() { const sidebar = document.querySelector(".window-sidebar") const button = document.createElement('span'); button.classList.add('button-link'); @@ -13,17 +13,17 @@ function addButton(){ const buttonText = document.createElement('span'); buttonText.innerText = 'Toggl Task'; button.append(buttonText); - + sidebar.querySelector(".mod-no-top-margin").classList.remove("mod-no-top-margin") sidebar.querySelector(".js-sidebar-add-heading").classList.remove("mod-no-top-margin") } const mappings = { - "GG": "gg", - "Audience Builder": "ab", - "Audience Explorer": "ae", - "Cta Calls": "xcta", - "Camper": "camper", + "GG": "gg", + "Audience Builder": "ab", + "Audience Explorer": "ae", + "Cta Calls": "xcta", + "Camper": "camper", }; function onClick() { @@ -32,29 +32,38 @@ function onClick() { 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 + 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"]) { - var labelShort = mappings[response["labels"][i]["name"]] - console.log(labelShort) + 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"]]}".`) + } } if (labelShort != undefined) { - const fixedEncodeURIComponent = (str) => { - return encodeURIComponent(str).replace(/[!'()*]/g, (c) => { - return '%' + c.charCodeAt(0).toString(16); - }); - } - var url = 'https://api.trello.com/1/cards/' + longId + '?name=' + fixedEncodeURIComponent(response["name"]) + fixedEncodeURIComponent(" #") + labelShort + "_" + shortId + '&key=' + apiKey + '&token=' + token + + + const url = new URL(`https://api.trello.com/1/cards/${longId}`) console.log(url) - fetch(url, { - method: 'PUT' - }).then(response => {//response.json()).then(response => { + + 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("HURRA!") }) } }