Easy-Session-Manager/src/scripts/listener.js

75 lines
2.1 KiB
JavaScript

const onMessageListener = async (request, sender, sendResponse) => {
switch (request.action) {
case "new-window": {
let store = request.store;
let newWindow = await browser.windows.create({focused: false});
for (let i = 0; i < store.length; i++) {
let createOption = (store[i].link !== "about:newtab") ?
{
active: false,
discarded: true,
pinned: false,
url: store[i].link,
windowId: newWindow.id,
index: i + 1
}
: { };
browser.tabs.create(createOption).catch( async (e) => {
createOption.url = returnReplaceURL(
"open_faild",
store[i].title,
store[i].link,
"../images/icons/error.png"
);
await browser.tabs.create(createOption);
});
}
let tabs = await browser.tabs.query({currentWindow: true});
browser.tabs.update(tabs.at(-1).id, { active: true });
browser.tabs.remove( tabs.at(0).id )
}
}
}
const returnReplaceURL = (state, title, url, favIconUrl) => {
let retUrl =
"/pages/replaced.html"
+ "?state="
+ encodeURIComponent(state)
+ "&title="
+ encodeURIComponent(title)
+ "&url="
+ encodeURIComponent(url)
+ "&favIconUrl="
+ encodeURIComponent(favIconUrl)
+ "&theme=dark";
// Reader mode
if (url.startsWith("about:reader?url=")) {
retUrl =
"/pages/replaced.html?state="
+ encodeURIComponent(state)
+ "&title="
+ encodeURIComponent(title)
+ "&url="
+ url.slice(17)
+ "&favIconUrl="
+ encodeURIComponent(favIconUrl)
+ "&openInReaderMode=true&theme=dark"
+ "&theme=dark";
}
return retUrl;
}
browser.runtime.onMessage.addListener(onMessageListener);