-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
174 lines (147 loc) · 6.77 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
function openViewer(opt) {
if (typeof browser !== typeof undefined && typeof browser.tabs !== typeof undefined) {
browser.tabs.query({
currentWindow: true,
active: true
}).then(onLoadTab).catch(onError);
return;
}
if (typeof chrome !== typeof undefined && typeof chrome.tabs !== typeof undefined) {
chrome.tabs.query({
currentWindow: true,
active: true
}, onLoadTab);
return;
}
onError("Can't load 'chrome.tabs' or 'browser.tabs'");
function onLoadTab(tabs) {
for (let tab of tabs) {
if (typeof browser !== typeof undefined && typeof browser.tabs !== typeof undefined) {
browser.tabs
.sendMessage(
tab.id, {
url: tab.url,
action: "show",
options: opt
}).then(response => {}).catch(onError);
} else if (typeof chrome !== typeof undefined && typeof chrome.tabs !== typeof undefined) {
chrome.tabs
.sendMessage(
tab.id, {
url: tab.url,
action: "show",
options: opt
});
}
}
}
}
function load() {
if (typeof browser !== typeof undefined && typeof browser.storage !== typeof undefined) {
let getting = browser.storage.local.get(["minWidthShow", "minHeightShow", "orderDefault", "orderDefaultDirection", "duplicates", "openAuto"]);
getting.then(onGot, onError);
} else if (typeof chrome !== typeof undefined && typeof chrome.storage !== typeof undefined) {
chrome.storage.local.get(["minWidthShow", "minHeightShow", "orderDefault", "orderDefaultDirection", "duplicates", "openAuto"], onGot);
} else {
onError("Can't load 'chrome.storage.local' or 'browser.storage.local'");
return;
}
function onGot(item) {
let iOptToSend = {
minWidthShow: typeof item.minWidthShow !== typeof undefined ? parseInt(item.minWidthShow, 10) : 32,
minHeightShow: typeof item.minHeightShow !== typeof undefined ? parseInt(item.minHeightShow, 10) : 32,
orderDefault: typeof item.orderDefault !== typeof undefined ? item.orderDefault : "DESC",
orderDefaultDirection: typeof item.orderDefaultDirection !== typeof undefined ? item.orderDefaultDirection : "A01",
duplicates: typeof item.duplicates !== typeof undefined ? item.duplicates : 0,
openAuto: typeof item.openAuto === typeof 'string' ? item.openAuto : ''
};
openViewer(iOptToSend);
}
}
function tabsController(tabId, action, changeInfo, tab) {
if (typeof browser !== typeof undefined && typeof browser.storage !== typeof undefined) {
let getting = browser.storage.local.get(["minWidthShow", "minHeightShow", "orderDefault", "orderDefaultDirection", "duplicates", "openAuto"]);
getting.then((item) => sendMessageToWebpage(item, tabId, action, changeInfo, tab), onError);
} else if (typeof chrome !== typeof undefined && typeof chrome.storage !== typeof undefined) {
chrome.storage.local.get(["minWidthShow", "minHeightShow", "orderDefault", "orderDefaultDirection", "duplicates", "openAuto"], (item) => sendMessageToWebpage(item, tabId, action, changeInfo, tab));
} else {
onError("Can't load 'chrome.storage.local' or 'browser.storage.local'");
return;
}
}
/**
* Send message to webpage
* @param {*} item browser|chrome.storage.local.get result
* @param {*} tabId browser|chrome.tabs.onUpdated.addListener result
* @param {*} action
* @param {*} changeInfo browser|chrome.tabs.onUpdated.addListener result
* @param {*} tab
*/
function sendMessageToWebpage(item, tabId, action, changeInfo, tab) {
let iOptToSend = {
minWidthShow: typeof item.minWidthShow !== typeof undefined ? parseInt(item.minWidthShow, 10) : 32,
minHeightShow: typeof item.minHeightShow !== typeof undefined ? parseInt(item.minHeightShow, 10) : 32,
orderDefault: typeof item.orderDefault !== typeof undefined ? item.orderDefault : "DESC",
orderDefaultDirection: typeof item.orderDefaultDirection !== typeof undefined ? item.orderDefaultDirection : "A01",
duplicates: typeof item.duplicates !== typeof undefined ? item.duplicates : 0,
openAuto: typeof item.openAuto === typeof 'string' ? item.openAuto : ''
};
if (typeof browser !== typeof undefined && typeof browser.tabs !== typeof undefined) {
browser.tabs
.sendMessage(
tabId, {
url: changeInfo.url,
action: action,
options: iOptToSend
}).then(response => {}).catch(onError);
return;
}
if (typeof chrome !== typeof undefined && typeof chrome.tabs !== typeof undefined) {
try {
chrome.tabs
.sendMessage(
tabId, {
// url: changeInfo.url,
action: action,
options: iOptToSend
}, (r) => console.log(r));
} catch (ex) {
}
return;
}
console.warn('Can\'t find "chrome.tabs.sendMessage" or "browser.tabs.sendMessage" or tabId', tabId);
}
function onError(error) {
console.warn(`Error: ${error}`);
}
if (typeof browser !== typeof undefined && typeof browser.browserAction !== typeof undefined) {
browser.browserAction.onClicked.addListener(load);
try {
browser.contextMenus.create({
id: "vz-view-gallery",
title: 'Image reader',
contexts: ["all"],
onclick: load
});
} catch (ex) {
console.warn(ex);
}
browser.tabs.onCreated.addListener((tabId, changeInfo, tab) => tabsController(tabId, "created", changeInfo, tab));
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => tabsController(tabId, "updated", changeInfo, tab));
} else if (typeof chrome !== typeof undefined && typeof chrome.browserAction !== typeof undefined) {
chrome.browserAction.onClicked.addListener(load);
try {
chrome.contextMenus.create({
id: "vz-view-gallery",
title: 'Image reader',
contexts: ["all"],
onclick: load
});
} catch (ex) {
console.warn(ex);
}
chrome.tabs.onCreated.addListener((tabId, changeInfo, tab) => tabsController(tabId, "created", changeInfo, tab));
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => tabsController(tabId, "updated", changeInfo, tab));
} else {
onError("Can't load 'chrome.browserAction.onClicked' or 'browser.browserAction.onClicked'");
}