-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
91 lines (74 loc) · 2.62 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
const defaultPreferences = {
windowLeft: 0,
windowTop: 0,
windowWidth: 450,
windowHeight: 170
};
let preferences;
// Load preferences from storage
browser.storage.local.get('preferences', (result) => {
preferences = result.preferences || structuredClone(defaultPreferences);
});
let originalWindow = 1;
const storePreferences = (windowInfo) => {
preferences.windowTop = windowInfo.top;
preferences.windowLeft = windowInfo.left;
preferences.windowWidth = windowInfo.width;
preferences.windowHeight = windowInfo.height;
browser.storage.local.set({ preferences: preferences });
}
const resetPreferences = (windowId) => {
preferences = structuredClone(defaultPreferences)
browser.storage.local.set({ preferences: preferences });
chrome.windows.update(windowId, { top: preferences.windowTop, left: preferences.windowLeft, width: preferences.windowWidth, height: preferences.windowHeight });
}
const popupWindow = (tab) => {
let top = preferences.windowTop;
let left = preferences.windowLeft;
let width = preferences.windowWidth;
let height = preferences.windowHeight;
let settings = {
type: 'popup',
top: top,
left: left,
width: width,
height: height,
tabId: tab.id
};
chrome.windows.create(settings, windowInfo => {
chrome.windows.update(windowInfo.id, { focused: false, top: top, left: left, width: width, height: height });
});
};
const mergeTab = (tabId, windowId) => {
//create new window if the original has been closed
chrome.windows.get(windowId, function (window) {
if (chrome.runtime.lastError) {
chrome.windows.create({ tabId: tabId, width: 1000, height: 800 });
return;
}
});
chrome.tabs.move(tabId, { windowId: windowId, index: -1 });
chrome.windows.update(windowId, { focused: true });
chrome.tabs.update(tabId, { active: true });
}
chrome.runtime.onMessage.addListener(function (msg, sender) {
if (msg.text == "spopupfy") {
originalWindow = sender.tab.windowId;
popupWindow(sender.tab);
}
else if (msg.text == "backToSpotify") {
// get window size and position and store them in local storage
// chrome.windows.get(sender.tab.windowId, function (window) {
// storePreferences({ top: window.top, left: window.left, width: window.width, height: window.height });
// });
mergeTab(sender.tab.id, originalWindow);
}
else if (msg.text == "resetInfo") {
resetPreferences(sender.tab.windowId);
}
else if(msg.text == "saveInfo"){
chrome.windows.get(sender.tab.windowId, function (window) {
storePreferences({ top: window.top, left: window.left, width: window.width, height: window.height });
});
}
});