-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
48 lines (37 loc) · 1.3 KB
/
popup.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
const SAFEWAY_COUPON_PAGE_URL = "https://www.safeway.com/foru/coupons-deals.html";
function modifyDOM() {
console.log('Tab script:');
console.log(document.body);
return document.body.innerHTML;
}
async function getCurrentTab() {
let queryOptions = { active: true, lastFocusedWindow: true };
// `tab` will either be a `tabs.Tab` instance or `undefined`.
let [tab] = await chrome.tabs.query(queryOptions);
return tab;
}
async function clipCoupons() {
const [tab] = await chrome.tabs.query({active: true, lastFocusedWindow: true});
if (tab !== undefined) {
console.log(tab);
if (tab.url === SAFEWAY_COUPON_PAGE_URL) {
const response = await chrome.tabs.sendMessage(tab.id, {message: "clipCoupons"});
// do something with response here, not outside the function
console.log(response);
} else {
// Open a tab
await chrome.tabs.create({url: SAFEWAY_COUPON_PAGE_URL});
}
} else {
console.log("It is undefined");
}
}
function clipCouponButtonPressed() {
clipCoupons();
}
function main() {
document.getElementById("press-me-btn").addEventListener("click", clipCouponButtonPressed)
}
document.addEventListener('DOMContentLoaded', function() {
main();
}, false);