-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
72 lines (57 loc) · 1.65 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
/* global STORAGE_NAME_EXIFDATA */
const CONTEXT_MENU_ID = 'ip-img-properties';
browser.menus.create({
id: CONTEXT_MENU_ID,
title: browser.i18n.getMessage('ContextMenuItem'),
contexts: ['image']
});
function OpenImagePropertiesWindow () {
function loadEXIFData (data) {
let exifData = JSON.parse(data[STORAGE_NAME_EXIFDATA]);
let isEXIFAvailable;
isEXIFAvailable = (Object.entries(exifData).length === 0 && exifData.constructor === Object) ? false : true;
let windowHeight = (isEXIFAvailable) ? 620 : 284;
browser.windows.create({
url: ['ui/classic.html'],
height: windowHeight,
width: 540,
type: 'popup'
});
}
let exifDataLoad = browser.storage.local.get(STORAGE_NAME_EXIFDATA);
exifDataLoad.then(loadEXIFData, onError);
}
browser.menus.onClicked.addListener(contextMenuClick);
browser.menus.onHidden.addListener(contextMenuHide);
browser.runtime.onMessage.addListener(notify);
function contextMenuClick(info, tab) {
if (info.menuItemId === CONTEXT_MENU_ID) {
if (info.mediaType !== 'image')
return;
try {
messageContentScript(info, tab);
}
catch (error) {
onError(error);
}
}
}
function contextMenuHide() {
browser.tabs.query({active: true})
.then (
function(tabs) {
browser.tabs.sendMessage(tabs[0].id, 'MenuHidden');
}
);
}
function notify(message) {
if (message == 'ImgDataFinished') {
OpenImagePropertiesWindow();
}
}
function messageContentScript(info, tab) {
browser.tabs.sendMessage(tab.id, 'ImgPropertiesClicked').catch(onError);
}
function onError(error) {
console.error('ImageProperties Background Error: ' + error.message);
}