-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
89 lines (77 loc) · 3.07 KB
/
background.js
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
function checkForUpdates() {
fetch('https://ediz.paste.lol/status-bookmarklet/raw')
.then(response => response.json())
.then(data => {
const currentVersion = chrome.runtime.getManifest().version;
const newVersion = data.version;
if (compareVersions(currentVersion, newVersion) < 0) {
const changelogLines = data.changelog.split('\n').filter(line => line.trim() !== '');
chrome.storage.local.set({
version: newVersion,
changelog: changelogLines,
url: data.url
}, () => {
chrome.tabs.create({ url: chrome.runtime.getURL('update.html') });
});
}
})
.catch(error => console.error('Update check failed:', error));
}
function compareVersions(v1, v2) {
const v1Parts = v1.split('.').map(Number);
const v2Parts = v2.split('.').map(Number);
while (v1Parts.length < v2Parts.length) v1Parts.push(0);
while (v2Parts.length < v1Parts.length) v2Parts.push(0);
for (let i = 0; i < v1Parts.length; i++) {
if (v1Parts[i] > v2Parts[i]) return 1;
if (v1Parts[i] < v2Parts[i]) return -1;
}
return 0;
}
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === "install" || details.reason === "update") {
checkForUpdates();
}
chrome.contextMenus.create({
id: "myContextMenuItem",
title: "status.lol Bookmarklet",
contexts: ["page", "selection", "link"]
});
});
chrome.runtime.onStartup.addListener(() => {
checkForUpdates();
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "myContextMenuItem") {
chrome.storage.sync.get('address', (data) => {
const address = data.address || 'foobar';
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs.length > 0) {
const title = encodeURIComponent(tabs[0].title);
const url = encodeURIComponent(tabs[0].url);
const link = `https://home.omg.lol/address/${address}/statuslog-bookmarklet?title=${title}&url=${url}`;
chrome.windows.create({
url: link,
type: "popup",
width: 700,
height: 670,
});
}
});
});
}
});
chrome.action.onClicked.addListener((tab) => {
chrome.storage.sync.get('address', (data) => {
const address = data.address || 'foobar';
const title = encodeURIComponent(tab.title);
const url = encodeURIComponent(tab.url);
const link = `https://home.omg.lol/address/${address}/statuslog-bookmarklet?title=${title}&url=${url}`;
chrome.windows.create({
url: link,
type: "popup",
width: 700,
height: 670,
});
});
});