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

Commit 126cedc

Browse files
author
WendelinPraktikum
authored
Merge pull request #6 from aboutsource/feature/5260
Feature/5260
2 parents 085770d + 08f1446 commit 126cedc

6 files changed

Lines changed: 133 additions & 87 deletions

File tree

content_script.js

Lines changed: 94 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
let engine;
2+
3+
if (window.browser !== undefined) {
4+
engine = browser;
5+
} else {
6+
engine = chrome;
7+
}
8+
19
function addButton() {
210
const sidebar = document.querySelector(".window-sidebar")
311
const button = document.createElement('span');
@@ -19,7 +27,6 @@ function addButton() {
1927
}
2028

2129
const mappings = {
22-
"Globalgold": "gg",
2330
"GG": "gg",
2431
"Audience Builder": "ab",
2532
"Audience Exporter": "ae",
@@ -29,110 +36,115 @@ const mappings = {
2936
};
3037

3138
function onClick() {
32-
let apiKey = "***"
33-
//generate token at https://trello.com/1/authorize?expiration=never&scope=read,write,account&response_type=token&name=Server%20Token&key=afadffe77f745496f80ebb4bf460c615
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"]
41-
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"]]}".`)
50-
}
51-
}
52-
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-
}
39+
let trelloApiKey = "afadffe77f745496f80ebb4bf460c615"
40+
engine.storage.local.get().then(result => {
41+
const trelloToken = result.trello
42+
const togglToken = result.toggl
7843

79-
})
80-
81-
82-
}
83-
84-
url = new URL("https://api.track.toggl.com/api/v8/workspaces")
85-
const togglAuth = btoa(`${togglToken}:api_token`)
44+
const idLong = window.location.pathname.split("/")[2];
45+
var url = new URL(`https://api.trello.com/1/cards/${idLong}`)
8646
fetch(url.toString(), {
8747
headers: {
88-
'Authorization': `Basic ${togglAuth}`,
48+
'Authorization': `OAuth oauth_consumer_key="${trelloApiKey}", oauth_token="${trelloToken}"`,
8949
'Content-Type': 'application/json'
90-
}
50+
},
9151
}).then(response => response.json()).then(response => {
92-
for (var i in response) {
93-
if (response[i]["name"] == "aboutsource") {
94-
var wid = response[i]["id"]
52+
const idShort = response["idShort"]
53+
54+
var labelShort, labelLong = undefined
55+
for (var i in response["labels"]) {
56+
if (labelShort == undefined) {
57+
labelShort = mappings[response["labels"][i]["name"]]
58+
labelLong = (labelShort != undefined) ? response["labels"][i]["name"] : labelLong
59+
} else if (mappings[response["labels"][i]["name"]] != undefined) {
60+
alert(`Diese Karte hat sowohl "${labelShort}" als auch "${mappings[response["labels"][i]["name"]]}".`)
9561
}
9662
}
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.")
63+
64+
if (labelShort == undefined) {
65+
alert("Diese Karte besitzt kein unterstütztes Projekt Label.")
9966
return false
10067
}
10168

102-
url = new URL(`https://api.track.toggl.com/api/v8/workspaces/${wid}/projects`)
69+
if (!response["name"].endsWith(`#${labelShort}_${idShort}`)) {
70+
71+
72+
url = new URL(`https://api.trello.com/1/cards/${idLong}`)
73+
74+
var data = {
75+
"name": `${response["name"]} #${labelShort}_${idShort}`
76+
};
77+
fetch(url.toString(), {
78+
method: 'PUT',
79+
headers: {
80+
'Authorization': `OAuth oauth_consumer_key="${trelloApiKey}", oauth_token="${trelloToken}"`,
81+
'Content-Type': 'application/json'
82+
},
83+
body: JSON.stringify(data)
84+
}).then(response => {
85+
if (!response.ok) {
86+
alert(`Error ${response.status}:\n${response.statusText}`)
87+
return false
88+
}
89+
90+
})
91+
92+
93+
}
94+
95+
url = new URL("https://api.track.toggl.com/api/v8/workspaces")
96+
const togglAuth = btoa(`${togglToken}:api_token`)
10397
fetch(url.toString(), {
10498
headers: {
10599
'Authorization': `Basic ${togglAuth}`,
106100
'Content-Type': 'application/json'
107101
}
108102
}).then(response => response.json()).then(response => {
109103
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
104+
if (response[i]["name"] == "aboutsource") {
105+
var wid = response[i]["id"]
106+
}
107+
}
108+
if (wid == undefined) {
109+
alert("WorkspaceID undefined\nDies bedeutet, du hast entweder keinen Zugriff zum a:s toggl oder es ist kein API-Token angegeben.")
110+
return false
111+
}
112+
113+
url = new URL(`https://api.track.toggl.com/api/v8/workspaces/${wid}/projects`)
114+
fetch(url.toString(), {
115+
headers: {
116+
'Authorization': `Basic ${togglAuth}`,
117+
'Content-Type': 'application/json'
118+
}
119+
}).then(response => response.json()).then(response => {
120+
for (var i in response) {
121+
if (response[i]["name"].endsWith(`(${labelShort})`)) {
122+
var pid = response[i]["id"]
123+
124+
url = new URL('https://api.track.toggl.com/api/v8/tasks')
125+
data = {
126+
"task": {
127+
"name": `${labelShort}_${idShort}`,
128+
"pid": pid
129+
}
118130
}
131+
fetch(url.toString(), {
132+
method: 'POST',
133+
headers: {
134+
'Authorization': `Basic ${togglAuth}`,
135+
'Content-Type': 'application/json'
136+
},
137+
body: JSON.stringify(data)
138+
})
119139
}
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-
})
130140
}
131-
}
141+
})
132142
})
133-
})
134143

144+
})
135145
})
146+
147+
136148
}
137149

138150
window.addEventListener("pushstate", function () {

icons/64.png

2.65 KB
Loading

icons/icon.png

-2.99 KB
Binary file not shown.

manifest.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Browser-Extension zum automatischen Anlegen von Toggl tracking tasks",
55
"version": "0.0.1",
66
"icons": {
7-
"64": "icons/icon.png"
7+
"64": "icons/64.png"
88
},
99

1010
"content_scripts": [
@@ -20,7 +20,8 @@
2020

2121
"permissions": [
2222
"*://api.trello.com/*",
23-
"*://*.toggl.com/*"
23+
"*://*.toggl.com/*",
24+
"storage"
2425
],
2526

2627
"options_ui": {

options/index.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4+
<link rel="icon" href="icons/64.png">
5+
<title>Trektor settings</title>
46
<meta charset="UTF-8">
57
<link href="style.css" rel="stylesheet" />
68
</head>
7-
<body>
8-
<h1>Extension settings</h1>
9+
<body class="browser-style">
10+
<h2>Trektor settings</h2>
11+
12+
<a href="https://trello.com/1/authorize?expiration=never&scope=read,write,account&response_type=token&name=Trektor&key=afadffe77f745496f80ebb4bf460c615">
13+
Trello Token generieren</a><br>
14+
<input type="text" id='trello_token' class="browser-style" aria-label="Trello Token"/><br><a href="https://track.toggl.com/profile">Toggl Token (ziemlich weit runter scrollen)</a> <br>
15+
<input type="text" id="toggl_token"/><br><br><br>
916
<script src="script.js"></script>
1017
</body>
1118
</html>

options/script.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,27 @@
1-
//extension settings script
1+
let engine;
2+
3+
if (window.browser !== undefined) {
4+
engine = browser;
5+
} else {
6+
engine = chrome;
7+
}
8+
9+
const trelloTextField = document.querySelector("#trello_token")
10+
const togglTextField = document.querySelector("#toggl_token")
11+
12+
trelloTextField.addEventListener("input", onChange)
13+
togglTextField.addEventListener("input", onChange)
14+
15+
function onChange() {
16+
engine.storage.local.set({
17+
"trello": trelloTextField.value,
18+
"toggl": togglTextField.value
19+
})
20+
console.log(`set trello token to ${togglTextField.value}`)
21+
console.log(`set toggl token to ${togglTextField.value}`)
22+
}
23+
24+
engine.storage.local.get().then(result => {
25+
trelloTextField.value = result["trello"]
26+
togglTextField.value = result["toggl"]
27+
})

0 commit comments

Comments
 (0)