forked from ngti/svg-grabber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
46 lines (38 loc) · 1.25 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
var id = 100;
// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
// Send a message to the active tab
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
var activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id, {
message: "clicked_browser_action"
});
});
});
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
var viewTabUrl = chrome.extension.getURL("getsvgs.html?id=" + id++);
var targetId = null;
var data = request.message;
var title = sender.tab.title;
var pageUrl = sender.tab.url
if (!title) {
title = sender.tab.url;
}
chrome.tabs.onUpdated.addListener(function listener(tabId, changedProps) {
if (tabId != targetId || changedProps.status != "complete") return;
chrome.tabs.onUpdated.removeListener(listener);
var views = chrome.extension.getViews();
for (var i = 0; i < views.length; i++) {
var view = views[i];
if (view.location.href == viewTabUrl) {
view.setSvgsUrl(data, title, pageUrl);
break;
}
}
});
if (request.message.type === "open_new_tab") {
chrome.tabs.create({ url: viewTabUrl }, function(tab) {
targetId = tab.id;
});
}
});