-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbackground.js
96 lines (81 loc) · 2.84 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
// Constants
var MINI_YOUTUBE_ACTIVATED = 'miniYouTubeActivated';
var MINI_SCREEN_LAST_TOP = 'miniScreenLastTop';
var MINI_SCREEN_LAST_LEFT = 'miniScreenLastLeft';
var MINI_SCREEN_LAST_HEIGHT = 'miniScreenLastHeight';
var MINI_SCREEN_LAST_WIDTH = 'miniScreenLastWidth';
var miniYouTubeActivated = true;
// Try if Edge browser object exists, else default to chrome
var browser = self.browser;
if (typeof browser === "undefined") {
browser = self.chrome;
}
// Try cloud sync, else fallback to localstorage
var storage = browser.storage.sync;
if (typeof storage === "undefined") {
storage = browser.storage.local;
}
// Check if Mini YouTube is enabled
storage.get([MINI_YOUTUBE_ACTIVATED], function(items) {
if (items[MINI_YOUTUBE_ACTIVATED])
miniYouTubeActivated = items[MINI_YOUTUBE_ACTIVATED];
});
// Update the icon according to the status
updateIcon();
// Receive and handle message from popup
browser.runtime.onMessage.addListener(function(request, sender, sendResponse) {
for (var message in request) {
switch (message) {
case 'update_icon':
// 1. Update status
setActivationStatus(request[message]);
// 2. Update icon
updateIcon();
break;
case 'get_activation_status':
sendResponse({"is_active": getActivationStatus()});
break;
case 'get_miniscreen_positions':
getMiniScreenPositions(sendResponse);
return true; // Keep channel open
case 'update_miniscreen_positions':
updateMiniScreenPositions(request[message]);
break;
default:
break;
}
}
});
function updateIcon() {
var iconPath = "images/icon128.png";
if (!miniYouTubeActivated)
iconPath = "images/icon128_grey.png";
browser.browserAction.setIcon({
path : {
"19": iconPath,
"38": iconPath
}
});
}
function getActivationStatus() {
return miniYouTubeActivated;
}
function setActivationStatus(isActive) {
miniYouTubeActivated = isActive;
storage.set({"miniYouTubeActivated": miniYouTubeActivated});
// Send message to each tab with youtube to update the status in the content script
browser.tabs.query({url: "https://www.youtube.com/*"},function(tabs){
tabs.forEach(function(tab){
browser.tabs.sendMessage(tab.id, {"update_activation_status": miniYouTubeActivated});
});
});
}
function getMiniScreenPositions(sendResponse) {
storage.get([MINI_SCREEN_LAST_TOP, MINI_SCREEN_LAST_LEFT,
MINI_SCREEN_LAST_HEIGHT, MINI_SCREEN_LAST_WIDTH], function(items){
sendResponse({"positions": items});
});
}
function updateMiniScreenPositions(positions) {
storage.set(positions);
}