-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
97 lines (83 loc) · 2.51 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
// Copyright (c) 2019 YourDigitalRights.org. All rights reserved.
// Use of this source code is governed by a GPLv3-style license that can be
// found in the LICENSE file.
try {
importScripts('psl.min.js');
} catch (e) {
console.error(e);
}
chrome.runtime.onInstalled.addListener(() => {
loadDomains();
});
chrome.runtime.onStartup.addListener(() => {
loadDomains();
});
chrome.tabs.onUpdated.addListener((tabId, props) => {
updateState(tabId);
});
chrome.tabs.onActivated.addListener((tab, props) => {
updateState(tab.tabId);
});
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
updateState(tabs[0].id);
});
chrome.action.onClicked.addListener((activeTab) => {
openOptOutURL(activeTab.url);
});
const whatBrowser = typeof browser !== "undefined" ? 'firefox' : 'chrome';
const ext = () => {
if (typeof browser !== "undefined") {
return browser;
}
return chrome;
}
const loadDomains = () => {
fetch('https://api.yourdigitalrights.org/domains')
.then((response) => {
return response.json();
})
.then((responnce) => {
var domainList = responnce['Domains'].map((domain) => domain.url);
chrome.storage.local.set({ domainList });
});
};
const openOptOutURL = async (url) => {
console.log(psl);
const hostname = new URL(url).hostname;
const parsed = psl.parse(hostname);
const { domainList } = await chrome.storage.local.get(["domainList"]);
if (domainList.includes(parsed.domain)) {
const newURL = `https://yourdigitalrights.org/d/${parsed.domain}?pk_campaign=browser-extension&pk_kwd=${whatBrowser}&pk_source=${parsed.domain}`;
chrome.tabs.create({url: newURL});
}
};
const setTab = async (tab, tabId) => {
try {
const hostname = new URL(tab.url).hostname;
const parsed = psl.parse(hostname);
const { domainList } = await chrome.storage.local.get(["domainList"]);
if (domainList.includes(parsed.domain)) {
ext().action.setTitle({title: `Click to send ${parsed.domain} a data Access or Deletion request`, tabId});
} else {
ext().action.setTitle({title: "This website is not on our list, click to send a custom request", tabId});
chrome.action.setPopup({tabId, popup: 'popup.html'})
}
} catch (e) {
console.log(e)
}
};
const getTab = async (tabId) => {
if (typeof browser !== "undefined") {
const tab = await browser.tabs.get(tabId);
setTab(tab, tabId);
} else {
chrome.tabs.get(tabId, (tab) => {
setTab(tab, tabId);
});
}
};
const updateState = (tabId) => {
if (tabId) {
getTab(tabId);
}
};