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 pathtrektor.js
More file actions
138 lines (113 loc) · 3.18 KB
/
Copy pathtrektor.js
File metadata and controls
138 lines (113 loc) · 3.18 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
class ChromeStorage {
constructor(storage) {
this.storage = storage;
}
get(keys) {
return new Promise((resolve) => {
this.storage.get(keys, resolve);
})
}
set(values) {
return new Promise((resolve) => {
this.storage.set(values, resolve);
})
}
}
class ChromeRuntime {
constructor(runtime) {
this.runtime = runtime;
}
sendMessage(msg) {
return new Promise((resolve) => {
this.runtime.sendMessage(msg, resolve);
});
}
get onMessage() {
const runtime = this.runtime;
return {
addListener(callback) {
runtime.onMessage.addListener((msg, sender, respond) => {
callback(msg)
.then((response) => { respond(response) })
.catch((error) => respond(error.message));
return true;
});
},
}
}
}
class TrelloGateway {
static ENDPOINT = "https://api.trello.com/1";
static API_KEY = "afadffe77f745496f80ebb4bf460c615";
constructor(storage) {
this.storage = storage;
}
getCard(id) {
return this.request("get", `/cards/${id}`);
}
updateCard(id, data) {
return this.request("put", `/cards/${id}`, data);
}
async request(method, path, data = null) {
const url = this.constructor.ENDPOINT + path;
const { trello: token } = await this.storage.get(["trello"]);
const response = await fetch(url, {
method,
headers: {
"Authorization": `OAuth oauth_consumer_key="${this.constructor.API_KEY}", oauth_token="${token}"`,
"Content-Type": "application/json; charset=utf-8",
},
body: (data === null) ? null : JSON.stringify(data),
});
return response.json();
}
}
class TogglGateway {
static ENDPOINT = "https://api.track.toggl.com/api/v8";
constructor(storage) {
this.storage = storage;
}
getWorkspaces() {
return this.request("get", "/workspaces");
}
getProjects(workspaceId) {
return this.request("get", `/workspaces/${workspaceId}/projects`);
}
getTasks(projectId) {
return this.request("get", `/projects/${projectId}/tasks`);
}
createTask(projectId, name) {
return this.request("post", "/tasks", {
task: { name, pid: projectId },
});
}
startTimeEntry(taskId) {
return this.request("post", "/time_entries/start", {
time_entry: { tid: taskId, created_with: "trektor" },
});
}
async request(method, path, data = null) {
const url = this.constructor.ENDPOINT + path;
const { toggl: token } = await this.storage.get(["toggl"]);
const response = await fetch(url, {
method,
headers: {
"Authorization": `Basic ${btoa(`${token}:api_token`)}`,
"Content-Type": "application/json; charset=utf-8",
},
body: (data === null) ? null : JSON.stringify(data),
});
return response.json();
}
}
const trektor = {};
if (window.chrome !== undefined) {
trektor.storage = new ChromeStorage(chrome.storage.local);
trektor.runtime = new ChromeRuntime(chrome.runtime);
} else {
trektor.storage = browser.storage.local;
trektor.runtime = browser.runtime;
}
trektor.trelloGateway = new TrelloGateway(trektor.storage);
trektor.togglGateway = new TogglGateway(trektor.storage);
window.trektor = trektor;