This repository was archived by the owner on Nov 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_script.js
More file actions
154 lines (130 loc) · 5.58 KB
/
Copy pathcontent_script.js
File metadata and controls
154 lines (130 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
let engine;
if (window.browser !== undefined) {
engine = browser;
} else {
engine = chrome;
}
function addButton() {
const sidebar = document.querySelector(".window-sidebar")
const button = document.createElement('span');
button.classList.add('button-link');
button.addEventListener('click', onClick);
sidebar.prepend(button);
const buttonIcon = document.createElement('span');
buttonIcon.classList.add('icon-sm', 'plugin-icon');
buttonIcon.innerText = '+';
button.append(buttonIcon);
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 Exporter": "ae",
"Cta Calls": "xcta",
"Camper": "camper",
"Praktikum": "pr"
};
function onClick() {
let trelloApiKey = "afadffe77f745496f80ebb4bf460c615"
engine.storage.local.get().then(result => {
const trelloToken = result.trello
const togglToken = result.toggl
const idLong = window.location.pathname.split("/")[2];
var url = new URL(`https://api.trello.com/1/cards/${idLong}`)
fetch(url.toString(), {
headers: {
'Authorization': `OAuth oauth_consumer_key="${trelloApiKey}", oauth_token="${trelloToken}"`,
'Content-Type': 'application/json'
},
}).then(response => response.json()).then(response => {
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 (labelShort == undefined) {
alert("Diese Karte besitzt kein unterstütztes Projekt 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="${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}`,
'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)
})
}
}
})
})
})
})
}
window.addEventListener("pushstate", function () {
if (window.location.pathname.startsWith("/c/")) {
addButton()
}
})