-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
44 lines (40 loc) · 1.63 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
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if(message.type === "STORE_SCRAPED_DATA") {
chrome.storage.local.set({ scrapedData: message.data }, () => {
// console.log("Scraped data successfully stored in Chrome storage:", message.data);
});
}
if(message.type === "GET_SCRAPED_DATA") {
chrome.storage.local.get(["scrapedData"], (result) => {
// console.log("Retrieved scraped data from storage:", result.scrapedData);
sendResponse(result.scrapedData || []);
});
return true;
}
if(message.type === "SCRAPE_ASSIGNMENTS") {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const tab = tabs[0];
//check if valid url
if(tab && tab.url.includes("sit.instructure.com")) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["content.js"]
});
} else {
// console.log("SCRAPE_ASSIGNMENTS attempted on an invalid page:", tab ? tab.url : "No active tab");
}
});
}
if(message.type === "LOGIN") {
chrome.identity.getAuthToken({interactive: true}, (token) => {
if(chrome.runtime.lastError) {
console.error("Auth Error:", chrome.runtime.lastError.message);
sendResponse({success: false, error: chrome.runtime.lastError});
return;
}
// console.log("Token:", token);
sendResponse({success: true, token})
});
return true;
}
});