TreeTabsMigration/scripts/refresh.js

333 lines
12 KiB
JavaScript
Raw Normal View History

2017-11-12 22:00:40 +01:00
// Copyright (c) 2017 kroppy. All rights reserved.
// Use of this source code is governed by a Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0) license
// that can be found at https://creativecommons.org/licenses/by-nc-nd/4.0/
// ********** REFRESH GUI ***************
2018-05-22 02:11:29 +02:00
async function RefreshGUI() {
2018-03-13 14:39:34 +01:00
let toolbar = document.getElementById("toolbar");
2018-05-22 02:11:29 +02:00
let toolbarHeight = 27;
2018-03-13 14:39:34 +01:00
if (toolbar.children.length > 0) {
toolbar.style.height = "";
toolbar.style.width = "";
toolbar.style.display = "";
toolbar.style.border = "";
toolbar.style.padding = "";
if (document.querySelector(".on.button") != null) {
toolbar.style.height = "53px";
2018-05-22 02:11:29 +02:00
toolbarHeight = 54;
2017-11-12 22:00:40 +01:00
} else {
2018-03-13 14:39:34 +01:00
toolbar.style.height = "26px";
2017-11-12 22:00:40 +01:00
}
} else {
2018-03-13 14:39:34 +01:00
toolbar.style.height = "0px";
2018-05-22 02:11:29 +02:00
toolbarHeight = 0;
2018-03-13 14:39:34 +01:00
toolbar.style.width = "0px";
toolbar.style.display = "none";
toolbar.style.border = "none";
toolbar.style.padding = "0";
2017-11-12 22:00:40 +01:00
}
2018-05-22 02:11:29 +02:00
let group_list = document.getElementById("group_list");
group_list.style.width = document.body.clientWidth + 50 + "px";
2018-03-13 14:39:34 +01:00
let pin_list = document.getElementById("pin_list");
if (pin_list.children.length > 0) {
2018-05-22 02:11:29 +02:00
pin_list.style.top = toolbarHeight + "px";
2018-03-13 14:39:34 +01:00
pin_list.style.height = "";
2018-05-22 02:11:29 +02:00
pin_list.style.width = "";
2018-03-13 14:39:34 +01:00
pin_list.style.display = "";
pin_list.style.border = "";
pin_list.style.padding = "";
2017-11-12 22:00:40 +01:00
} else {
2018-03-13 14:39:34 +01:00
pin_list.style.top = "0px";
pin_list.style.height = "0px";
pin_list.style.width = "0px";
pin_list.style.display = "none";
pin_list.style.border = "none";
pin_list.style.padding = "0";
2017-11-12 22:00:40 +01:00
}
2018-05-22 02:11:29 +02:00
let pin_listHeight = pin_list.getBoundingClientRect().height;
2018-03-13 14:39:34 +01:00
let toolbar_groups = document.getElementById("toolbar_groups");
2018-05-22 02:11:29 +02:00
toolbar_groups.style.top = toolbarHeight + pin_listHeight + "px";
toolbar_groups.style.height = document.body.clientHeight - toolbarHeight - pin_listHeight + "px";
let toolbar_groupsWidth = toolbar_groups.getBoundingClientRect().width;
2018-03-13 14:39:34 +01:00
if (opt.show_counter_groups) {
document.querySelectorAll(".group").forEach(function(s){
let groupLabel = document.getElementById("_gte"+s.id);
if (groupLabel) {
groupLabel.textContent = (bggroups[s.id] ? bggroups[s.id].name : caption_noname_group) + " (" + document.querySelectorAll("#"+s.id+" .tab").length + ")";
}
});
} else {
document.querySelectorAll(".group").forEach(function(s){
let groupLabel = document.getElementById("_gte"+s.id);
if (groupLabel) {
groupLabel.textContent = bggroups[s.id] ? bggroups[s.id].name : caption_noname_group;
}
});
}
document.querySelectorAll(".group_button").forEach(function(s){
s.style.height = s.firstChild.getBoundingClientRect().height + "px";
2017-11-12 22:00:40 +01:00
});
2018-03-13 14:39:34 +01:00
let groups = document.getElementById("groups");
2018-05-22 02:11:29 +02:00
let groupsHeight = document.body.clientHeight - toolbarHeight - pin_listHeight;
groups.style.top = toolbarHeight + pin_listHeight + "px";
groups.style.left = toolbar_groupsWidth + "px";
groups.style.height = groupsHeight + "px";
groups.style.width = (document.body.clientWidth - toolbar_groupsWidth - 1) + "px";
// let total = ManagerWindowGroupsListHeight + ManagerWindowSessionsListHeight;
// if (total > document.body.clientHeight - 200) {
// let cut = (total - (document.body.clientHeight - 200))/2;
// ManagerWindowGroupsList.style.height = ManagerWindowGroupsListHeight - cut + "px";
// ManagerWindowSessionsList.style.height = ManagerWindowSessionsListHeight - cut + "px";
// } else {
// ManagerWindowGroupsList.style.height = ManagerWindowGroupsListHeight + "px";
// ManagerWindowSessionsList.style.height = ManagerWindowSessionsListHeight + "px";
// }
// ManagerWindow.style.height = ManagerWindowGroupsList.clientHeight + ManagerWindowSessionsList.clientHeight + 300 + "px";
let PanelList = document.querySelector(".mw_pan_on>.manager_window_list");
let PanelListHeight = 3 + PanelList.children.length * 18;
let ManagerWindowPanelButtons = document.querySelector(".mw_pan_on>.manager_window_panel_buttons");
let ManagerWindowPanelButtonsHeight = ManagerWindowPanelButtons.clientHeight;
let MaxAllowedHeight = document.body.clientHeight - 140;
if (PanelListHeight + ManagerWindowPanelButtonsHeight < MaxAllowedHeight) {
PanelList.style.height = PanelListHeight + "px";
} else {
PanelList.style.height = MaxAllowedHeight - ManagerWindowPanelButtonsHeight + "px";
}
// if (ManagerWindowSessionsListHeight > document.body.clientHeight - 300) {
// ManagerWindowSessionsList.style.height = document.body.clientHeight - 300 + "px";
// } else {
// ManagerWindowSessionsList.style.height = ManagerWindowSessionsListHeight + "px";
// }
let ManagerWindow = document.getElementById("manager_window");
// ManagerWindow.style.height = PanelListHeight.clientHeight + 700 + "px";
ManagerWindow.style.height = PanelList.clientHeight + ManagerWindowPanelButtonsHeight + 56 + "px";
2017-11-12 22:00:40 +01:00
}
// set discarded class
function RefreshDiscarded(tabId) {
2018-03-13 14:39:34 +01:00
let t = document.getElementById(tabId);
if (t != null) {
2017-11-12 22:00:40 +01:00
chrome.tabs.get(parseInt(tabId), function(tab) {
if (tab) {
if (tab.discarded) {
2018-03-13 14:39:34 +01:00
t.classList.add("discarded");
2017-11-12 22:00:40 +01:00
} else {
2018-03-13 14:39:34 +01:00
t.classList.remove("discarded");
2017-11-12 22:00:40 +01:00
}
}
});
}
}
// set discarded class
function SetAttentionIcon(tabId) {
2018-03-13 14:39:34 +01:00
let t = document.getElementById(tabId);
if (t != null) {
t.classList.add("attention");
2017-11-12 22:00:40 +01:00
}
}
// change media icon
function RefreshMediaIcon(tabId) {
2018-03-13 14:39:34 +01:00
let t = document.getElementById(tabId);
if (t != null) {
2017-11-12 22:00:40 +01:00
chrome.tabs.get(parseInt(tabId), function(tab) {
if (tab) {
if (tab.mutedInfo.muted) {
2018-03-13 14:39:34 +01:00
t.classList.remove("audible");
t.classList.add("muted");
2017-11-12 22:00:40 +01:00
}
if (!tab.mutedInfo.muted && tab.audible) {
2018-03-13 14:39:34 +01:00
t.classList.remove("muted");
t.classList.add("audible");
2017-11-12 22:00:40 +01:00
}
if (!tab.mutedInfo.muted && !tab.audible) {
2018-03-13 14:39:34 +01:00
t.classList.remove("audible");
t.classList.remove("muted");
2017-11-12 22:00:40 +01:00
}
}
});
}
}
// Vivaldi does not have changeInfo.audible listener, this is my own implementation, hopefully this will not affect performance too much
function VivaldiRefreshMediaIcons() {
2017-11-20 18:14:07 +01:00
setInterval(function() {
2018-05-22 02:11:29 +02:00
chrome.tabs.query({currentWindow: true, audible: true}, function(tabs) {
2018-03-13 14:39:34 +01:00
document.querySelectorAll(".audible, .muted").forEach(function(s){
s.classList.remove("audible");
s.classList.remove("muted");
});
2017-11-20 18:14:07 +01:00
let tc = tabs.length;
for (var ti = 0; ti < tc; ti++) {
if (tabs[ti].audible) {
2018-03-13 14:39:34 +01:00
document.getElementById(tabs[ti].id).classList.add("audible");
2017-11-20 18:14:07 +01:00
}
if (tabs[ti].mutedInfo.muted) {
2018-03-13 14:39:34 +01:00
document.getElementById(tabs[ti].id).classList.add("muted");
2017-11-20 18:14:07 +01:00
}
2017-11-12 22:00:40 +01:00
}
2017-11-20 18:14:07 +01:00
});
2018-05-22 02:11:29 +02:00
}, 2000);
2017-11-12 22:00:40 +01:00
}
2018-05-22 02:11:29 +02:00
async function GetFaviconAndTitle(tabId, addCounter) {
2018-03-13 14:39:34 +01:00
let t = document.getElementById(tabId);
if (t != null) {
2017-11-12 22:00:40 +01:00
chrome.tabs.get(parseInt(tabId), function(tab) {
if (tab){
2017-11-20 18:14:07 +01:00
let title = tab.title ? tab.title : tab.url;
2018-05-22 02:11:29 +02:00
let tHeader = t.childNodes[0];
2018-03-13 14:39:34 +01:00
let tTitle = tHeader.childNodes[1];
2017-11-12 22:00:40 +01:00
if (tab.status == "complete") {
2018-03-13 14:39:34 +01:00
t.classList.remove("loading");
2017-11-12 22:00:40 +01:00
// change title
2018-03-13 14:39:34 +01:00
tTitle.textContent = title;
tHeader.title = title;
tHeader.setAttribute("tabTitle", title);
2017-11-12 22:00:40 +01:00
// compatibility with various Tab suspender extensions
if (tab.favIconUrl != undefined && tab.favIconUrl.match("data:image/png;base64") != null) {
2018-03-13 14:39:34 +01:00
tHeader.style.backgroundImage = "url(" + tab.favIconUrl + ")";
2017-11-12 22:00:40 +01:00
} else {
// case for internal pages, favicons don't have access, but can be loaded from url
2017-11-20 18:14:07 +01:00
if (tab.url.match("opera://|vivaldi://|browser://|chrome://|chrome-extension://") != null) {
2018-03-13 14:39:34 +01:00
tHeader.style.backgroundImage = "url(chrome://favicon/" + tab.url + ")";
2017-11-12 22:00:40 +01:00
} else {
// change favicon
2018-03-13 14:39:34 +01:00
let img = new Image();
2017-11-12 22:00:40 +01:00
img.src = tab.favIconUrl;
img.onload = function() {
2018-03-13 14:39:34 +01:00
tHeader.style.backgroundImage = "url(" + tab.favIconUrl + ")";
2017-11-12 22:00:40 +01:00
};
img.onerror = function() {
2018-03-13 14:39:34 +01:00
tHeader.style.backgroundImage = ((tab.url == "" || browserId == "F") ? "url(./theme/icon_empty.svg)" : ("url(chrome://favicon/" + tab.url + ")"));
// "url(" + tab.url + ")"
2017-11-12 22:00:40 +01:00
}
}
}
}
if (tab.status == "loading") {
title = tab.title ? tab.title : caption_loading;
2018-03-13 14:39:34 +01:00
t.classList.add("loading");
tHeader.style.backgroundImage = "";
tHeader.title = caption_loading;
tHeader.setAttribute("tabTitle", caption_loading);
tTitle.textContent = caption_loading;
2017-11-12 22:00:40 +01:00
setTimeout(function() {
2018-03-13 14:39:34 +01:00
if (document.getElementById(tab.id) != null) GetFaviconAndTitle(tab.id, addCounter);
2017-11-12 22:00:40 +01:00
}, 1000);
}
2018-01-01 19:50:56 +01:00
if (addCounter && (opt.show_counter_tabs || opt.show_counter_tabs_hints)) {
RefreshTabCounter(tabId);
2017-11-20 18:14:07 +01:00
}
2017-11-12 22:00:40 +01:00
}
});
}
}
// refresh open closed trees states
2018-05-22 02:11:29 +02:00
async function RefreshExpandStates() {
2018-03-13 14:39:34 +01:00
document.querySelectorAll("#"+active_group+" .folder").forEach(function(s){
2018-05-22 02:11:29 +02:00
if (s.childNodes[1].children.length == 0 && s.childNodes[2].children.length == 0) {
2018-03-13 14:39:34 +01:00
s.classList.remove("o");
s.classList.remove("c");
2018-01-01 19:50:56 +01:00
} else {
2018-03-13 14:39:34 +01:00
if (s.classList.contains("o") == false && s.classList.contains("c") == false) {
s.classList.add("o");
2017-11-20 18:14:07 +01:00
}
2018-01-01 19:50:56 +01:00
}
});
2018-03-13 14:39:34 +01:00
document.querySelectorAll("#"+active_group+" .tab").forEach(function(s){
2018-05-22 02:11:29 +02:00
if (s.childNodes[1].children.length == 0) {
2018-03-13 14:39:34 +01:00
s.classList.remove("o");
s.classList.remove("c");
2017-11-12 22:00:40 +01:00
} else {
2018-03-13 14:39:34 +01:00
if (s.classList.contains("o") == false && s.classList.contains("c") == false) {
s.classList.add("o");
2017-11-20 18:14:07 +01:00
}
2017-11-12 22:00:40 +01:00
}
});
2017-11-20 18:14:07 +01:00
}
2018-05-22 02:11:29 +02:00
async function RefreshCounters() {
2018-01-01 19:50:56 +01:00
if (opt.show_counter_tabs || opt.show_counter_tabs_hints) {
2018-03-13 14:39:34 +01:00
document.querySelectorAll("#"+active_group+" .tab").forEach(function(s){
2018-05-22 02:11:29 +02:00
let title = s.childNodes[0].getAttribute("tabTitle");
2018-03-13 14:39:34 +01:00
if (title != null) {
2018-05-22 02:11:29 +02:00
s.childNodes[0].title = title;
s.childNodes[0].childNodes[1].textContent =title;
2018-01-01 19:50:56 +01:00
}
});
2018-03-13 14:39:34 +01:00
document.querySelectorAll("#"+active_group+" .o.tab, #"+active_group+" .c.tab").forEach(function(s){
2018-05-22 02:11:29 +02:00
let title = s.childNodes[0].getAttribute("tabTitle");
2018-03-13 14:39:34 +01:00
if (opt.show_counter_tabs && title != null) {
2018-05-22 02:11:29 +02:00
s.childNodes[0].childNodes[1].textContent = ("("+ document.querySelectorAll("[id='" + s.id + "'] .tab").length +") ") + title;
2018-01-01 19:50:56 +01:00
}
if (opt.show_counter_tabs_hints) {
2018-05-22 02:11:29 +02:00
s.childNodes[0].title = ("("+ document.querySelectorAll("[id='" + s.id + "'] .tab").length +") ") + title;
2018-01-01 19:50:56 +01:00
}
});
2018-03-13 14:39:34 +01:00
document.querySelectorAll("#"+active_group+" .folder").forEach(function(s){
if (opt.show_counter_tabs && bgfolders[s.id]) {
2018-05-22 02:11:29 +02:00
s.childNodes[0].childNodes[1].textContent = ("("+ document.querySelectorAll("[id='" + s.id + "'] .tab").length +") ") + bgfolders[s.id].name;
2018-01-01 19:50:56 +01:00
}
2018-03-13 14:39:34 +01:00
if (opt.show_counter_tabs_hints && bgfolders[s.id]) {
2018-05-22 02:11:29 +02:00
s.childNodes[0].title = ("("+ document.querySelectorAll("[id='" + s.id + "'] .tab").length +") ") + bgfolders[s.id].name;
2018-01-01 19:50:56 +01:00
}
});
}
}
2018-05-22 02:11:29 +02:00
async function RefreshTabCounter(tabId) {
2018-03-13 14:39:34 +01:00
let t = document.getElementById(tabId);
2018-05-22 02:11:29 +02:00
if (t.childNodes[0]) {
let title = t.childNodes[0].getAttribute("tabTitle");
if (t != null && title != null) {
if (t.classList.contains("o") || t.classList.contains("c")) {
if (opt.show_counter_tabs) {
t.childNodes[0].childNodes[1].textContent = ("("+ document.querySelectorAll("[id='" + t.id + "'] .tab").length +") ") + title;
}
if (opt.show_counter_tabs_hints) {
t.childNodes[0].title = ("("+ document.querySelectorAll("[id='" + t.id + "'] .tab").length +") ") + title;
}
} else {
t.childNodes[0].title = title;
t.childNodes[0].childNodes[1].textContent = title;
2017-11-20 18:14:07 +01:00
}
}
}
}