-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
73 lines (61 loc) · 2.36 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
/* background.js */
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "glassdoorSalaries",
title: "Search Glassdoor Salaries",
contexts: ["selection"]
});
});
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
if (info.menuItemId === "glassdoorSalaries") {
try {
const query = encodeURIComponent(info.selectionText);
const searchUrl = `https://www.glassdoor.com/Search/results.htm?keyword=${query}`;
const response = await fetch(searchUrl);
const data = await response.text();
if (!response.ok) {
throw new Error(`Network response was not ok: ${response.statusText}`);
}
// Use scripting API to execute content script on the tab
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: parseGlassdoorHTML,
args: [data],
});
} catch (error) {
handleError(error.message);
}
}
});
// Error handling function
function handleError(message) {
console.error("Error fetching Glassdoor page:", message);
chrome.notifications.create('', {
type: "basic",
iconUrl: "icon.png", // Ensure this path is correct
title: "Glassdoor Salaries Extension",
message: `Error: ${message}`,
});
}
// Content script function to parse HTML
function parseGlassdoorHTML(htmlString) {
const parser = new DOMParser();
const doc = parser.parseFromString(htmlString, "text/html");
console.log("HTML: ", doc)
const resultLink = doc.querySelector('a[data-test="company-tile"]');
if (resultLink) {
const overviewURL = "https://www.glassdoor.com/" + resultLink.getAttribute("href");
const idMatch = overviewURL.match(/E\d+/);
const nameMatch = overviewURL.match(/Working-at-(.*?)-EI/);
if (idMatch && nameMatch) {
const id = idMatch[0];
const companyName = nameMatch[1];
const salariesURL = `https://www.glassdoor.com/Salary/${companyName}-Salaries-${id}.htm`;
window.open(salariesURL, '_blank');
} else {
console.log("Failed to extract company details.");
}
} else {
console.log("Salaries link not found. Please try with a different company name.");
}
}