-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
43 lines (40 loc) · 1.34 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
function generateCsv(products) {
var csvContent = 'data:text/csv;charset=utf-8,';
csvContent += 'Name,Price,Currency\n';
products.forEach(function(product) {
csvContent += [product.name, product.price.value, product.price.currency].join(',') + '\n';
});
return csvContent;
}
function downloadCsv(csvContent) {
var encodedUri = encodeURI(csvContent);
window.open(encodedUri);
}
chrome.pageAction.onClicked.addListener(function(tab) {
var tabId = tab.id;
chrome.tabs.executeScript(tabId, { file: "content.js" }, function() {
chrome.tabs.sendMessage(tabId, {}, function(results) {
var myCsv = generateCsv(results);
downloadCsv(myCsv);
});
});
});
// When the extension is installed or upgraded ...
chrome.runtime.onInstalled.addListener(function() {
// Replace all rules ...
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
// With a new rule ...
chrome.declarativeContent.onPageChanged.addRules([
{
// That fires when a page's URL contains 'amazon' ...
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { urlContains: 'amazon.' },
})
],
// And shows the extension's page action.
actions: [ new chrome.declarativeContent.ShowPageAction() ]
}
]);
});
});