-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
110 lines (101 loc) · 3.51 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
let title, fullHTMLTEXT = '';
let matchingTitles = [];
let badgeTextValues = {};
let currentTabId, tabUrl = null
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'getCurrentTabId') {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
currentTabId = tabs[0].id;
tabUrl = tabs[0].url;
sendResponse({ tabId: currentTabId, tabUrl: tabUrl });
});
return true;
}
if (request.action === 'matchingTitleSelected') {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, {
action: 'matchingTitleSelected',
payload: request.payload,
flair: request.flair,
clrFlair: request.clrFlair
});
});
}
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const currentTab = tabs[0];
const tabId = currentTab.id;
if (tabId === tabId) {
const subredditName = 'worldwidecheck';
fetch(`https://www.reddit.com/r/${subredditName}/new.json`)
.then(response => {
return !response.ok ? new Error('Network response was not ok') : response.json();
})
.then(data => {
const posts = data.data.children;
posts.forEach(post => {
const postData = {
title: post.data.title
}
if (request.action === 'sendfullHTMLTEXT') {
updateFullHTMLText(request.fullTxt)
getPostTitle(postData.title, request.fullTxt)
}
});
const matchesBadges = badgeTextValues[tabId] || 0;
setTxtBadge(matchesBadges.toString(), tabId);
})
.catch(error => {
console.log('Error occurred while fetching subreddit posts:', error);
}).finally(() => {
})
function getPostTitle(newTitle, fulltxt) {
if (fulltxt.trim().toLowerCase().includes(newTitle.toLowerCase())) {
console.log(`Matching title found: ${newTitle}`);
badgeTextValues[tabId] = (badgeTextValues[tabId] || 0) + 1;
matchingTitles.push(newTitle)
} else {
console.log('no match found')
}
}
}
function setTxtBadge(badgeTextValues, tabId) {
if (request.action === 'matchingTitleSelected') {
if (badgeTextValues > 0) {
chrome.action.setBadgeText({ text: badgeTextValues, tabId: tabId });
chrome.action.setBadgeBackgroundColor({ color: request.clrFlair, tabId: tabId });
showNotification();
}
}
}
});
if (request.action === 'openPopupInCurrentTab') {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const currentTab = tabs[0];
if (currentTab) {
chrome.scripting.executeScript({
target: { tabId: currentTab.id },
function: () => {
chrome.runtime.sendMessage({ action: 'openPopup' });
},
});
}
});
}
badgeTextValues = {}
});
function updateFullHTMLText(newValue) {
if (fullHTMLTEXT !== newValue) {
fullHTMLTEXT = newValue;
}
}
function showNotification() {
const options = {
type: "basic",
title: "Online Information Warning ⚠️",
message: "World Wide Check community has reported suspicious information on this site! Open the extension to see it.",
iconUrl: "/img/path_256x256.png"
}
chrome.notifications.create(options, () => {
chrome.runtime.lastError ? console.error("Error creating notification:", chrome.runtime.lastError) : '';
});
}