Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
191 lines (155 loc) · 6.57 KB
/
Copy pathbackground.js
File metadata and controls
191 lines (155 loc) · 6.57 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// function processUrl(url_string) {
// // Remove 'http://' or 'https://'
// let processedUrl = url_string.replace(/^https?:\/\//i, '');
// // Add 'www.' if it's not present
// if (!processedUrl.startsWith('www.')) {
// processedUrl = 'www.' + processedUrl;
// }
// return processedUrl;
// }
function processUrl(url_string) {
// Remove 'http://' or 'https://'
const withoutProtocol = url_string.replace(/^https?:\/\//, '');
// Add 'www.' if not present
const withWWW = withoutProtocol.startsWith('www.') ? withoutProtocol : 'www.' + withoutProtocol;
// Extract the domain until the first occurrence of '/'
const domain = withWWW.split('/')[0];
return domain;
}
function logUrl(url_string) {
fetch('http://localhost:5000/log-url', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ url: url_string }),
})
.then(response => {
if (response.status == 500){
checkAndCloseWindow(url_string)
}})
.catch(error => {
console.error('log url - Fetch error:', error);
});
}
function checkAndCloseWindow(url_string, res) {
fetch('http://localhost:5000/check', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ url: url_string}),
})
.then(response =>response.json())
.then(data => {
var shouldCloseWindow = false;
url_string = processUrl(url_string);
console.log(`checkAndCloseWindow - url_string: ${url_string}`)
var return_val = { shouldCloseWindow: false, formattedUrl: url_string };
// Fetch micromanager_info from chrome.storage.local
chrome.storage.local.get('micromanagerInfo', function (data) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
}
const currentMicromanagerInfo = data.micromanagerInfo || { micromanage: false, allowed_websites: [] };
// Check if micromanage is active and the URL is not in allowed_websites
console.log(`Checking current status of micromanager: currentMicromanagerInfo.micromanage - ${currentMicromanagerInfo.micromanage}, currentMicromanagerInfo.allowed_websites - ${currentMicromanagerInfo.allowed_websites}`);
if (currentMicromanagerInfo.micromanage && !currentMicromanagerInfo.allowed_websites.includes(url_string)) {
// Implement your logic to close the window here
console.log(`Blocking access to URL: ${url_string}`);
shouldCloseWindow = true;
return_val = { shouldCloseWindow: true, formattedUrl: url_string };
if (shouldCloseWindow) {
// Get the current tab ID
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const currentTabId = tabs[0].id;
// Close the current tab
chrome.tabs.remove(currentTabId, function () {
// Check if there was an error
if (chrome.runtime.lastError) {
console.error('Error closing tab:', chrome.runtime.lastError);
}
});
});
};
} else {
// Implement your check logic here if needed
return_val = { shouldCloseWindow: false, formattedUrl: url_string};
}
});
console.log(`check response: ${data}`)
console.log(`shouldCloseWindow: ${shouldCloseWindow}, formattedUrl: ${url_string}`)
if (shouldCloseWindow) {
// Get the current tab ID
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const currentTabId = tabs[0].id;
// Close the current tab
chrome.tabs.remove(currentTabId, function () {
// Check if there was an error
if (chrome.runtime.lastError) {
console.error('Error closing tab:', chrome.runtime.lastError);
}
});
});
}
return return_val
})
.catch(error => {
console.error(error);
});
}
function blockWebsite(url, duration, sendResponse) {
// Fetch micromanager_info from chrome.storage.local
chrome.storage.local.get('micromanagerInfo', function (data) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
}
const setMicromanagerInfo = { micromanage: true, allowed_websites: [url]};
try {
// Save the updated micromanager_info to chrome.storage.local
chrome.storage.local.set(
{
micromanagerInfo: setMicromanagerInfo
},
function () {
// This function is a callback executed after the storage is set
// Fetch micromanager_info from chrome.storage.local again to get the updated value
chrome.storage.local.get('micromanagerInfo', function (data) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
}
const updatedMicromanagerInfo = data.micromanagerInfo || { micromanage: false, allowed_websites: [] };
console.log(`Blocked all websites except ${url} for ${duration} minutes`);
console.log(`After setting chrome.storage.local - micromanagerInfo.micromanage: ${updatedMicromanagerInfo.micromanage}, micromanagerInfo.allowed_websites: ${updatedMicromanagerInfo.allowed_websites}`);
// Check if micromanage is true and do something
if (updatedMicromanagerInfo.micromanage) {
console.log('Micromanage is active!');
}
// Sleep for the specified duration
setTimeout(function () {
// After the sleep duration, reset the micromanager_info
chrome.storage.local.set({ micromanagerInfo: { micromanage: false, allowed_websites: [] } }, function () {
console.log('Micromanager deactivated!!');
// Send a response to the popup.js indicating success
sendResponse({ success: true });
});
}, duration * 60 * 1000); // Convert minutes to milliseconds for setTimeout
});
}
);
} catch (error) {
console.error('Error blocking website:', error);
sendResponse({ success: false, error: error.message });
}
});
}
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action === "logUrl") {
logUrl(request.url);
} else if (request.action === "checkAndCloseWindow") {
checkAndCloseWindow(request.url);
} else if (request.action === "blockWebsite") {
blockWebsite(request.url, request.duration, sendResponse);
}
return true; // Use return true to indicate that sendResponse will be called asynchronously
});