Skip to content
This repository was archived by the owner on Nov 11, 2025. It is now read-only.

Commit 085770d

Browse files
author
WendelinPraktikum
authored
Merge pull request #5 from aboutsource/feature/5258
toggl-task anlegen
2 parents d46b7d7 + ef74fa3 commit 085770d

2 files changed

Lines changed: 104 additions & 39 deletions

File tree

content_script.js

Lines changed: 101 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,55 +19,119 @@ function addButton() {
1919
}
2020

2121
const mappings = {
22+
"Globalgold": "gg",
2223
"GG": "gg",
2324
"Audience Builder": "ab",
24-
"Audience Explorer": "ae",
25+
"Audience Exporter": "ae",
2526
"Cta Calls": "xcta",
2627
"Camper": "camper",
28+
"Praktikum": "pr"
2729
};
2830

2931
function onClick() {
30-
let apiKey = "afadffe77f745496f80ebb4bf460c615"
32+
let apiKey = "***"
3133
//generate token at https://trello.com/1/authorize?expiration=never&scope=read,write,account&response_type=token&name=Server%20Token&key=afadffe77f745496f80ebb4bf460c615
32-
let token = "***"
33-
console.log("Hallo")
34-
const longId = window.location.pathname.split("/")[2];
35-
var url = `https://api.trello.com/1/cards/${longId}?key=${apiKey}&token=${token}`
36-
fetch(url).then(response => response.json()).then(response => {
37-
shortId = response["idShort"]
38-
39-
if (!response["name"].endsWith(shortId)) {
40-
for (var i in response["labels"]) {
41-
if (labelShort == undefined) {
42-
var labelShort = mappings[response["labels"][i]["name"]]
43-
} else if (mappings[response["labels"][i]["name"]] != undefined) {
44-
alert(`Diese Karte hat sowohl "${labelShort}" als auch "${mappings[response["labels"][i]["name"]]}".`)
45-
}
46-
}
34+
let token = "af00d9597af9fb897f6b2fedb3b7152845991fe186caca08cf5dbb03bba40ffc"
35+
let togglToken = "***"
36+
37+
const idLong = window.location.pathname.split("/")[2];
38+
var url = new URL(`https://api.trello.com/1/cards/${idLong}?key=${apiKey}&token=${token}`)
39+
fetch(url.toString()).then(response => response.json()).then(response => {
40+
const idShort = response["idShort"]
4741

48-
if (labelShort != undefined) {
49-
50-
51-
const url = new URL(`https://api.trello.com/1/cards/${longId}`)
52-
console.log(url)
53-
54-
const data = {
55-
"name": `${response["name"]} #${labelShort}_${shortId}`
56-
};
57-
const jsonBody = JSON.stringify(data);
58-
fetch(url.toString(), {
59-
method: 'PUT',
60-
headers: {
61-
'Authorization': `OAuth oauth_consumer_key="${apiKey}", oauth_token="${token}"`,
62-
'Content-Type': 'application/json'
63-
},
64-
body: jsonBody
65-
}).then(response => {
66-
console.log(response)
67-
})
42+
console.log("Hallo")
43+
var labelShort, labelLong = undefined
44+
for (var i in response["labels"]) {
45+
if (labelShort == undefined) {
46+
labelShort = mappings[response["labels"][i]["name"]]
47+
labelLong = (labelShort != undefined) ? response["labels"][i]["name"] : labelLong
48+
} else if (mappings[response["labels"][i]["name"]] != undefined) {
49+
alert(`Diese Karte hat sowohl "${labelShort}" als auch "${mappings[response["labels"][i]["name"]]}".`)
6850
}
6951
}
7052

53+
if (labelShort == undefined) {
54+
alert("Diese Karte besitzt kein in den mappings vorhandenes Label.")
55+
return false
56+
}
57+
58+
if (!response["name"].endsWith(`#${labelShort}_${idShort}`)) {
59+
60+
61+
url = new URL(`https://api.trello.com/1/cards/${idLong}`)
62+
63+
var data = {
64+
"name": `${response["name"]} #${labelShort}_${idShort}`
65+
};
66+
fetch(url.toString(), {
67+
method: 'PUT',
68+
headers: {
69+
'Authorization': `OAuth oauth_consumer_key="${apiKey}", oauth_token="${token}"`,
70+
'Content-Type': 'application/json'
71+
},
72+
body: JSON.stringify(data)
73+
}).then(response => {
74+
if (!response.ok) {
75+
alert(`Error ${response.status}:\n${response.statusText}`)
76+
return false
77+
}
78+
79+
})
80+
81+
82+
}
83+
84+
url = new URL("https://api.track.toggl.com/api/v8/workspaces")
85+
const togglAuth = btoa(`${togglToken}:api_token`)
86+
fetch(url.toString(), {
87+
headers: {
88+
'Authorization': `Basic ${togglAuth}`,
89+
'Content-Type': 'application/json'
90+
}
91+
}).then(response => response.json()).then(response => {
92+
for (var i in response) {
93+
if (response[i]["name"] == "aboutsource") {
94+
var wid = response[i]["id"]
95+
}
96+
}
97+
if (wid == undefined) {
98+
alert("WorkspaceID undefined\nDies bedeutet, du hast entweder keinen Zugriff zum a:s toggl oder es ist kein API-Token angegeben.")
99+
return false
100+
}
101+
102+
url = new URL(`https://api.track.toggl.com/api/v8/workspaces/${wid}/projects`)
103+
fetch(url.toString(), {
104+
headers: {
105+
'Authorization': `Basic ${togglAuth}`,
106+
'Content-Type': 'application/json'
107+
}
108+
}).then(response => response.json()).then(response => {
109+
for (var i in response) {
110+
if (response[i]["name"].endsWith(`(${labelShort})`)) {
111+
var pid = response[i]["id"]
112+
113+
url = new URL('https://api.track.toggl.com/api/v8/tasks')
114+
data = {
115+
"task": {
116+
"name": `${labelShort}_${idShort}`,
117+
"pid": pid
118+
}
119+
}
120+
fetch(url.toString(), {
121+
method: 'POST',
122+
headers: {
123+
'Authorization': `Basic ${togglAuth}`,
124+
'Content-Type': 'application/json'
125+
},
126+
body: JSON.stringify(data)
127+
}).then(response => {
128+
console.log(response)
129+
})
130+
}
131+
}
132+
})
133+
})
134+
71135
})
72136
}
73137

manifest.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
],
2020

2121
"permissions": [
22-
"*://api.trello.com/*"
22+
"*://api.trello.com/*",
23+
"*://*.toggl.com/*"
2324
],
2425

2526
"options_ui": {
2627
"page": "options/index.html"
2728
}
28-
}
29+
}

0 commit comments

Comments
 (0)