-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
147 lines (124 loc) · 5.1 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
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
139
140
141
142
143
144
145
146
147
chrome.runtime.onInstalled.addListener(function () {
const defaultValues = {
innocentWebsites: [], // Innocent exepmpt sites database
replace_sites: [
{ url: 'https://www.khanacademy.org/' },
{ url: 'https://www.coursera.org/' },
{ url: 'https://www.udacity.com/' },
{ url: 'https://docs.google.com/' },
{ url: 'https://drive.google.com/' },
{ url: 'https://www.google.com/' },
{ url: 'https://mail.google.com/' },
{ url: 'https://calendar.google.com/' },
{ url: 'https://www.youtube.com/' },
{ url: 'https://stackoverflow.com/' },
{ url: 'https://www.microsoft.com/' },
{ url: 'https://office.live.com/' },
{ url: 'https://www.linkedin.com/' },
{ url: 'https://www.wikipedia.org/' },
{ url: 'https://news.ycombinator.com/' },
{ url: 'https://www.nytimes.com/' },
{ url: 'https://www.bbc.com/' },
{ url: 'https://www.nasa.gov/' },
{ url: 'https://www.economist.com/' },
{ url: 'https://www.nationalgeographic.com/' }
]
};
chrome.storage.local.set(defaultValues, function () {
console.log('Default values set in chrome.storage.local.');
});
});
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action === 'replaceHistory') {
replaceHistory();
} else if (request.action === 'removeFromHistory') {
removeFromHistory(request.url);
}
});
const contextMenuId = 'add-to-innocent-history';
chrome.contextMenus.create({
id: contextMenuId,
title: 'Add to Exempt sites',
contexts: ['page', 'link']
});
chrome.contextMenus.onClicked.addListener(function (info, tab) {
if (info.menuItemId === contextMenuId) {
addWebsiteToDatabase(info.pageUrl || info.linkUrl);
}
});
function addWebsiteToDatabase(url) {
chrome.storage.local.get({ innocentWebsites: [] }, function (result) {
const innocentWebsites = result.innocentWebsites || [];
const domain = extractDomain(url);
if (!innocentWebsites.some((site) => site.url === domain)) {
innocentWebsites.push({ url: domain, timestamp: Date.now() });
chrome.storage.local.set({ innocentWebsites }, function () {
console.log(`${domain} added to the Innocent History database.`);
});
} else {
console.log(`${domain} is already in the Innocent History database.`);
}
});
}
function extractDomain(url) {
// extract domain from URL
const match = url.match(/^https?:\/\/([^/?#]+)/i);
return match && match[1];
}
function removeFromHistory(url) {
console.log('Removing URL from history:', url);
chrome.history.deleteUrl({ url: url }, function () {
if (chrome.runtime.lastError) {
console.error('Error removing URL from history:', chrome.runtime.lastError);
return;
}
console.log(`${url} removed from browsing history.`);
});
}
function replaceHistory() {
chrome.storage.local.get({ replace_sites: [] }, function (result) {
const replaceSites = result.replace_sites || [];
if (replaceSites.length === 0) {
console.log('No websites to replace in history.');
return;
}
const today = new Date();
today.setHours(0, 0, 0, 0);
const tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 1);
tomorrow.setMilliseconds(tomorrow.getMilliseconds() - 1);
chrome.history.search({ text: '', startTime: today.getTime(), endTime: tomorrow.getTime() }, function (historyItems) {
if (chrome.runtime.lastError) {
console.error('Error in history search:', chrome.runtime.lastError);
return;
}
console.log('History items to be replaced:', historyItems);
historyItems.forEach((historyItem) => {
console.log('Replacing:', historyItem);
const matchingSite = true;
const randomSite = replaceSites[Math.floor(Math.random() * replaceSites.length)];
if (matchingSite) {
// remove from history
chrome.history.deleteUrl({ url: historyItem.url }, function () {
if (chrome.runtime.lastError) {
console.error('Error removing URL from history:', chrome.runtime.lastError);
return;
}
console.log(`${historyItem.url} removed from browsing history.`);
});
// add the fake site to history
chrome.history.addUrl({
url: randomSite.url
}, function () {
if (chrome.runtime.lastError) {
console.error('Error adding URL back to history:', chrome.runtime.lastError);
return;
}
console.log(`Added ${randomSite.url} to history.`);
});
}
});
console.log('Browser history replaced with predefined websites.');
});
});
}