-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
32 lines (25 loc) · 1.11 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
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (!changeInfo.url) return;
const url = changeInfo.url;
if (!url.includes("twitter.com") && !url.includes("reddit.com")) return;
const match = url.includes("twitter.com")
? url.match(/https:\/\/twitter\.com\/[^\/]+\/status\/(\d+)/)
: url.match(/https:\/\/www\.reddit\.com\/r\/[^\/]+\/comments\/(\w+)/);
if (!match) return;
const postId = match[1];
const storageKey = `myriadUrl_${tabId}`;
fetch('https://api.myriad.social/user/posts?pageLimit=200')
.then(response => response.json())
.then(data => {
const post = data.data.find(post => post.originPostId === postId);
if (!post) {
chrome.action.setBadgeText({text: '', tabId: tabId});
return;
}
const myriadUrl = `https://app.myriad.social/post/${post.id}`;
chrome.storage.local.set({[storageKey]: myriadUrl});
chrome.action.setBadgeBackgroundColor({color: [255, 0, 0, 255]});
chrome.action.setBadgeText({text: 'i', tabId: tabId});
})
.catch(error => console.error('Error:', error));
});