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

Commit efa3eed

Browse files
committed
introduce webextensions-polyfill
1 parent f8f0872 commit efa3eed

9 files changed

Lines changed: 54 additions & 79 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
web-ext-artifacts
2+
node_modules

background.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
trektor.runtime.onMessage.addListener(async (msg) => {
1+
browser.runtime.onMessage.addListener(async (msg) => {
22
switch (msg.action) {
33
case 'track':
44
await track(...msg.args);

content_script.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ async function addButton() {
3333
trackButton.addEventListener("click", async () => {
3434
trackButtonIcon.classList.add("trektor-state-loading");
3535

36-
const response = await trektor.runtime.sendMessage({
37-
action: "track",
38-
args: [window.location.pathname.split("/", 3)[2]],
36+
const response = await browser.runtime.sendMessage({
37+
action: "track",
38+
args: [window.location.pathname.split("/", 3)[2]],
3939
});
4040
trackButtonIcon.classList.remove("trektor-state-loading");
4141

@@ -48,7 +48,7 @@ async function addButton() {
4848
});
4949

5050
addButton.addEventListener("click", async () => {
51-
const response = await trektor.runtime.sendMessage({
51+
const response = await browser.runtime.sendMessage({
5252
action: "addTask",
5353
args: [window.location.pathname.split("/", 3)[2]],
5454
});

manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"https://trello.com/*"
1818
],
1919
"js": [
20-
"trektor.js",
20+
"node_modules/webextension-polyfill/dist/browser-polyfill.min.js",
2121
"content_script.js"
2222
],
2323
"css": [
@@ -27,6 +27,7 @@
2727
],
2828
"background": {
2929
"scripts": [
30+
"node_modules/webextension-polyfill/dist/browser-polyfill.min.js",
3031
"trektor.js",
3132
"background.js"
3233
]

options/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
<input type="text" name="toggl" />
3131

32-
<script src="../trektor.js"></script>
32+
<script src="../node_modules/webextension-polyfill/dist/browser-polyfill.min.js"></script>
3333
<script src="./script.js"></script>
3434
</body>
3535
</html>

options/script.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
document.querySelectorAll("input").forEach((field) => {
22
field.addEventListener("input", (e) => {
3-
trektor.storage.set({ [e.target.name]: e.target.value });
3+
browser.storage.local.set({ [e.target.name]: e.target.value });
44
});
55

6-
trektor.storage.get(field.name).then(({ [field.name]: value }) => {
6+
browser.storage.local.get(field.name).then(({ [field.name]: value }) => {
77
field.value = (value === undefined) ? '' : value;
88
});
99
});

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "trektor",
3+
"version": "0.0.3",
4+
"repository": "git@github.com:aboutsource/trektor",
5+
"author": "info@aboutsource.net",
6+
"license": "MIT",
7+
"dependencies": {
8+
"webextension-polyfill": "^0.8.0"
9+
}
10+
}

trektor.js

Lines changed: 25 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,24 @@
1-
class ChromeStorage {
2-
constructor(storage) {
3-
this.storage = storage;
4-
}
5-
get(keys) {
6-
return new Promise((resolve) => {
7-
this.storage.get(keys, resolve);
8-
})
9-
}
10-
set(values) {
11-
return new Promise((resolve) => {
12-
this.storage.set(values, resolve);
13-
})
14-
}
15-
}
16-
17-
class ChromeRuntime {
18-
constructor(runtime) {
19-
this.runtime = runtime;
20-
}
21-
22-
sendMessage(msg) {
23-
return new Promise((resolve) => {
24-
this.runtime.sendMessage(msg, resolve);
25-
});
26-
}
27-
28-
get onMessage() {
29-
const runtime = this.runtime;
30-
31-
return {
32-
addListener(callback) {
33-
runtime.onMessage.addListener((msg, sender, respond) => {
34-
callback(msg)
35-
.then((response) => { respond(response) })
36-
.catch((error) => respond(error.message));
37-
return true;
38-
});
39-
},
40-
}
41-
}
42-
}
43-
441
class TrelloGateway {
452
static ENDPOINT = "https://api.trello.com/1";
463
static API_KEY = "afadffe77f745496f80ebb4bf460c615";
474

5+
#storage;
6+
487
constructor(storage) {
49-
this.storage = storage;
8+
this.#storage = storage;
509
}
5110

5211
getCard(id) {
53-
return this.request("get", `/cards/${id}`);
12+
return this.#request("get", `/cards/${id}`);
5413
}
5514

5615
updateCard(id, data) {
57-
return this.request("put", `/cards/${id}`, data);
16+
return this.#request("put", `/cards/${id}`, data);
5817
}
5918

60-
async request(method, path, data = null) {
19+
async #request(method, path, data = null) {
6120
const url = this.constructor.ENDPOINT + path;
62-
const { trello: token } = await this.storage.get("trello");
21+
const { trello: token } = await this.#storage.get("trello");
6322

6423
const response = await fetch(url, {
6524
method,
@@ -81,37 +40,43 @@ class TrelloGateway {
8140
class TogglGateway {
8241
static ENDPOINT = "https://api.track.toggl.com/api/v8";
8342

43+
#storage;
44+
8445
constructor(storage) {
85-
this.storage = storage;
46+
this.#storage = storage;
8647
}
8748

8849
getWorkspaces() {
89-
return this.request("get", "/workspaces");
50+
return this.#request("get", "/workspaces");
9051
}
9152

9253
getProjects(workspaceId) {
93-
return this.request("get", `/workspaces/${workspaceId}/projects`);
54+
return this.#request("get", `/workspaces/${workspaceId}/projects`);
9455
}
9556

9657
getTasks(projectId) {
97-
return this.request("get", `/projects/${projectId}/tasks`);
58+
return this.#request("get", `/projects/${projectId}/tasks`);
9859
}
9960

10061
createTask(projectId, name) {
101-
return this.request("post", "/tasks", {
62+
return this.#request("post", "/tasks", {
10263
task: { name, pid: projectId },
10364
});
10465
}
10566

67+
getCurrentTimeEntry() {
68+
return this.#request("get", "/time_entries/current");
69+
}
70+
10671
startTimeEntry(taskId) {
107-
return this.request("post", "/time_entries/start", {
72+
return this.#request("post", "/time_entries/start", {
10873
time_entry: { tid: taskId, created_with: "trektor" },
10974
});
11075
}
11176

112-
async request(method, path, data = null) {
77+
async #request(method, path, data = null) {
11378
const url = this.constructor.ENDPOINT + path;
114-
const { toggl: token } = await this.storage.get("toggl");
79+
const { toggl: token } = await this.#storage.get("toggl");
11580

11681
const response = await fetch(url, {
11782
method,
@@ -130,17 +95,7 @@ class TogglGateway {
13095
}
13196
}
13297

133-
const trektor = {};
134-
135-
if (window.chrome !== undefined) {
136-
trektor.storage = new ChromeStorage(chrome.storage.local);
137-
trektor.runtime = new ChromeRuntime(chrome.runtime);
138-
} else {
139-
trektor.storage = browser.storage.local;
140-
trektor.runtime = browser.runtime;
141-
}
142-
143-
trektor.trelloGateway = new TrelloGateway(trektor.storage);
144-
trektor.togglGateway = new TogglGateway(trektor.storage);
145-
146-
window.trektor = trektor;
98+
window.trektor = {
99+
trelloGateway: new TrelloGateway(browser.storage.local),
100+
togglGateway: new TogglGateway(browser.storage.local),
101+
};

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
webextension-polyfill@^0.8.0:
6+
version "0.8.0"
7+
resolved "https://registry.yarnpkg.com/webextension-polyfill/-/webextension-polyfill-0.8.0.tgz#f80e9f4b7f81820c420abd6ffbebfa838c60e041"
8+
integrity sha512-a19+DzlT6Kp9/UI+mF9XQopeZ+n2ussjhxHJ4/pmIGge9ijCDz7Gn93mNnjpZAk95T4Tae8iHZ6sSf869txqiQ==

0 commit comments

Comments
 (0)