-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
138 lines (109 loc) · 3.35 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
chrome.runtime.onInstalled.addListener(function(info){
chrome.storage.local.set({
autodelete: true, host: "localhost"
}, function(items) {
});
});
let filename=""
let focus=0;
let atServer=false;
let serverTabId=-1;
let activeId=-1;
let host =""
var downloadIds=[]
var popIds=[]
chrome.storage.onChanged.addListener(function(changes, namespace) {
for (var key in changes) {
var storageChange = changes[key];
if(key=='host')
host=storageChange.newValue;
}
});
/*chrome.tabs.onRemoved.addListener(function(tabId,info){
if(serverTabId==tabId)
{
del=false;
chrome.storage.local.get('autodelete', function(data) {
del=data.autodelete;
var i=0
var len = downloadIds.length;
if(del)
for (; i < len; i++) {
chrome.downloads.removeFile(downloadIds[i]);
}
downloadIds=[]
});
}
});*/
chrome.windows.onRemoved.addListener(function(windowid) {
if(popIds.includes(windowid))
{
del=false;
chrome.storage.local.get('autodelete', function(data) {
del=data.autodelete;
var i=0
var len = downloadIds.length;
if(del)
for (; i < len; i++) {
chrome.downloads.removeFile(downloadIds[i]);
}
downloadIds=[]
});
popIds=popIds.splice(popIds.indexOf(windowid),1)
}
})
function checkForValidUrl(tabId, changeInfo, tab) {
// If the tabs url starts with "http://specificsite.com"...
if (tab.url.includes(host)) {
// ... show the page action.
chrome.pageAction.show(tabId);
serverTabId=tabId;
} else {
chrome.pageAction.hide(tabId);
}
};
// Listen for any changes to the URL of any tab.
chrome.tabs.onUpdated.addListener(checkForValidUrl);
chrome.tabs.onActiveChanged.addListener(function(tabId, selectInfo){
chrome.tabs.get(tabId, function(tab){
activeId=tabId;
var active=false;
if(tab.hasOwnProperty('pendingUrl'))
if(tab.pendingUrl.includes(host))
active=true;
if(tab.hasOwnProperty('url') )
if(tab.url.includes(host))
active=true;
if(active)
serverTabId=tabId;
});
});
chrome.downloads.onCreated.addListener(function(item){
chrome.storage.local.get('host', function(data) {
host=data.host;
url=item.finalUrl+item.url;
if(url.includes(host))
downloadIds.push(item.id);
});
});
chrome.downloads.onChanged.addListener(function(delta){
if(delta.hasOwnProperty('filename'))
filename=delta.filename.current;
var myProp = 'state';
if(delta.hasOwnProperty(myProp))
if(delta.state.current=="complete")
{
if(downloadIds.includes(delta.id))
{
var createData={url:"file:///"+filename};
chrome.windows.create(createData,function(win){
popIds.push(win.id)
tabId=win.tabs[0].id;
chrome.windows.update(win.id, { focused: true });
chrome.tabs.executeScript(
tabId,
{file: 'printDialog.js'});
});
}
}
});