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/
// ********** CHROME EVENTS ***************
2018-01-01 19:50:56 +01:00
function StartChromeListeners ( ) {
2018-05-22 02:11:29 +02:00
2018-01-01 19:50:56 +01:00
if ( browserId == "F" ) {
browser . browserAction . onClicked . addListener ( function ( tab ) {
if ( tab . windowId == CurrentWindowId ) {
browser . sidebarAction . close ( ) ;
}
} ) ;
}
2018-05-22 02:11:29 +02:00
2017-11-12 22:00:40 +01:00
chrome . runtime . onMessage . addListener ( function ( message , sender , sendResponse ) {
2018-05-22 02:11:29 +02:00
2018-01-01 19:50:56 +01:00
if ( message . command == "backup_available" ) {
2018-05-22 02:11:29 +02:00
if ( opt . debug ) {
log ( "message to sidebar " + CurrentWindowId + ": message: " + message . command ) ;
}
let BAKbutton = document . getElementById ( "button_load_bak" + message . bak ) ;
if ( BAKbutton != null ) {
BAKbutton . classList . remove ( "disabled" ) ;
}
2018-01-01 19:50:56 +01:00
}
2018-05-22 02:11:29 +02:00
2017-11-12 22:00:40 +01:00
if ( message . command == "drag_drop" ) {
2018-05-22 02:11:29 +02:00
if ( opt . debug ) {
log ( "message to sidebar " + CurrentWindowId + ": message: " + message . command ) ;
2018-01-01 19:50:56 +01:00
}
2018-05-22 02:11:29 +02:00
CleanUpDragClasses ( ) ;
DragNodeClass = message . DragNodeClass ;
DragTreeDepth = Object . assign ( 0 , message . DragTreeDepth ) ;
2017-11-12 22:00:40 +01:00
}
2018-05-22 02:11:29 +02:00
2018-03-13 14:39:34 +01:00
if ( message . command == "dragend" ) {
2018-05-22 02:11:29 +02:00
if ( opt . debug ) {
log ( "message to sidebar " + CurrentWindowId + ": message: " + message . command ) ;
}
2018-03-13 14:39:34 +01:00
CleanUpDragClasses ( ) ;
2018-05-22 02:11:29 +02:00
EmptyDragAndDrop ( ) ;
2018-03-13 14:39:34 +01:00
}
2018-05-22 02:11:29 +02:00
if ( message . command == "remove_folder" ) {
if ( opt . debug ) {
log ( "message to sidebar " + CurrentWindowId + ": message: " + message . command + " folderId: " + message . folderId ) ;
}
RemoveFolder ( message . folderId ) ;
}
2017-11-12 22:00:40 +01:00
if ( message . command == "reload_sidebar" ) {
2018-05-22 02:11:29 +02:00
if ( opt . debug ) {
log ( "message to sidebar " + CurrentWindowId + ": message: " + message . command ) ;
}
2017-11-12 22:00:40 +01:00
window . location . reload ( ) ;
}
2018-05-22 02:11:29 +02:00
2017-11-12 22:00:40 +01:00
if ( message . command == "reload_options" ) {
2018-05-22 02:11:29 +02:00
if ( opt . debug ) {
log ( "message to sidebar " + CurrentWindowId + ": message: " + message . command ) ;
}
opt = Object . assign ( { } , message . opt ) ;
setTimeout ( function ( ) {
RestorePinListRowSettings ( ) ;
} , 100 ) ;
}
if ( message . command == "reload_toolbar" ) {
if ( opt . debug ) {
log ( "message to sidebar " + CurrentWindowId + ": message: " + message . command ) ;
}
opt = Object . assign ( { } , message . opt ) ;
if ( opt . show _toolbar ) {
RemoveToolbar ( ) ;
RecreateToolbar ( message . toolbar ) ;
SetToolbarEvents ( false , true , true , "mousedown" ) ;
RestoreToolbarShelf ( ) ;
RestoreToolbarSearchFilter ( ) ;
} else {
RemoveToolbar ( ) ;
}
RefreshGUI ( ) ;
2017-11-12 22:00:40 +01:00
}
2018-05-22 02:11:29 +02:00
2017-11-12 22:00:40 +01:00
if ( message . command == "reload_theme" ) {
2018-05-22 02:11:29 +02:00
if ( opt . debug ) {
log ( "message to sidebar " + CurrentWindowId + ": message: " + message . command ) ;
}
RestorePinListRowSettings ( ) ;
ApplyTheme ( message . theme ) ;
2017-11-12 22:00:40 +01:00
}
2018-05-22 02:11:29 +02:00
2017-11-12 22:00:40 +01:00
if ( message . windowId == CurrentWindowId ) {
2018-03-13 14:39:34 +01:00
// I WANT TO MOVE THIS LOGIC TO THE BACKGROUND SCRIPT!
2018-05-22 02:11:29 +02:00
if ( message . command == "tab_created" ) {
2018-03-13 14:39:34 +01:00
2018-05-22 02:11:29 +02:00
chrome . tabs . get ( message . tabId , function ( NewTab ) { // get tab again as reported tab's url is empty! Also for some reason firefox sends tab with "active == false" even if tab is active (THIS IS POSSIBLY A NEW BUG IN FF 60.01!)
if ( opt . debug ) {
log ( "chrome event: tab_created: " + message . tabId ) ;
}
if ( opt . move _tabs _on _url _change == "from_empty_b" && NewTab . url == newTabUrl ) {
EmptyTabs . push ( message . tabId ) ;
2017-11-12 22:00:40 +01:00
}
2018-05-22 02:11:29 +02:00
if ( document . getElementById ( message . tabId ) == null ) {
if ( opt . move _tabs _on _url _change == "from_empty" && NewTab . url == newTabUrl ) {
EmptyTabs . push ( message . tabId ) ;
}
if ( message . parentTabId != undefined ) {
AppendTab ( NewTab , message . parentTabId , false , false , true , message . index , true , false , false , true , false ) ;
2018-01-01 19:50:56 +01:00
} else {
2018-05-22 02:11:29 +02:00
if ( opt . append _orphan _tab == "as_child" && NewTab . openerTabId == undefined && document . querySelector ( "#" + active _group + " .active_tab" ) ) {
if ( opt . debug ) {
log ( "tab_created: as_child, ignores orphan case, appending tab as child" ) ;
2018-01-01 19:50:56 +01:00
}
2018-05-22 02:11:29 +02:00
NewTab . openerTabId = document . querySelector ( "#" + active _group + " .active_tab" ) . id ;
2017-11-12 22:00:40 +01:00
}
2018-05-22 02:11:29 +02:00
if ( NewTab . openerTabId ) { // child case
if ( opt . append _child _tab == "after_active" ) {
if ( opt . debug ) {
log ( "tab_created: child case, tab will append after active, openerTabId: " + NewTab . openerTabId ) ;
}
AppendTab ( NewTab , false , false , document . querySelector ( "#" + active _group + " .active_tab" ) != null ? document . querySelector ( "#" + active _group + " .active_tab" ) . id : false , false , false , true , false , false , true , false ) ;
} else {
let Parents = GetParentsByClass ( document . getElementById ( NewTab . openerTabId ) , "tab" ) ;
if ( opt . max _tree _depth < 0 || ( opt . max _tree _depth > 0 && Parents . length < opt . max _tree _depth ) ) { // append to tree
if ( opt . append _child _tab == "top" ) {
if ( opt . debug ) {
log ( "tab_created: child case, in tree limit, tab will append on top, openerTabId: " + NewTab . openerTabId ) ;
}
AppendTab ( NewTab , NewTab . openerTabId , false , false , ( NewTab . pinned ? true : false ) , false , true , false , false , true , false ) ;
}
if ( opt . append _child _tab == "bottom" ) {
if ( opt . debug ) {
log ( "tab_created: child case, in tree limit, tab will append on bottom, openerTabId: " + NewTab . openerTabId ) ;
}
AppendTab ( NewTab , NewTab . openerTabId , false , false , true , false , true , false , false , true , false ) ;
}
}
if ( opt . max _tree _depth > 0 && Parents . length >= opt . max _tree _depth ) { // if reached depth limit of the tree
if ( opt . debug ) {
log ( "tab_created: child case, surpassed tree limit, openerTabId: " + NewTab . openerTabId ) ;
}
if ( opt . append _child _tab _after _limit == "after" ) {
if ( opt . debug ) {
log ( "tab_created: tab will append after active, openerTabId: " + NewTab . openerTabId ) ;
}
AppendTab ( NewTab , false , false , NewTab . openerTabId , true , false , true , false , false , true , false ) ;
}
if ( opt . append _child _tab _after _limit == "top" ) {
if ( opt . debug ) {
log ( "tab_created: tab will append on top, openerTabId: " + NewTab . openerTabId ) ;
}
AppendTab ( NewTab , document . getElementById ( NewTab . openerTabId ) . parentNode . parentNode . id , false , false , ( NewTab . pinned ? true : false ) , false , true , false , false , true , false ) ;
}
if ( opt . append _child _tab _after _limit == "bottom" ) {
if ( opt . debug ) {
log ( "tab_created: tab will append on bottom, openerTabId: " + NewTab . openerTabId ) ;
}
AppendTab ( NewTab , document . getElementById ( NewTab . openerTabId ) . parentNode . parentNode . id , false , false , true , false , true , false , false , true , false ) ;
}
}
2018-01-01 19:50:56 +01:00
}
2018-05-22 02:11:29 +02:00
if ( opt . max _tree _depth == 0 ) { // place tabs flat
if ( opt . debug ) {
log ( "tab_created: max_tree_depth is 0, tabs are placed on the same level" ) ;
}
if ( opt . append _child _tab _after _limit == "after" ) {
if ( opt . debug ) {
log ( "tab_created: tab will append after active" ) ;
}
AppendTab ( NewTab , false , false , NewTab . openerTabId , false , false , true , false , false , true , false ) ;
}
if ( opt . append _child _tab _after _limit == "top" ) {
if ( opt . debug ) {
log ( "tab_created: tab will append on top" ) ;
}
AppendTab ( NewTab , false , false , false , false , false , true , false , false , true , false ) ;
}
if ( opt . append _child _tab _after _limit == "bottom" ) {
if ( opt . debug ) {
log ( "tab_created: tab will append on bottom" ) ;
}
AppendTab ( NewTab , false , false , false , true , false , true , false , false , true , false ) ;
}
2018-01-01 19:50:56 +01:00
}
2018-05-22 02:11:29 +02:00
} else { // orphan case
// if set to append orphan tabs to ungrouped group
// if tab is still not present, basically, not opened by OpenNewTab(), it will switch to ungrouped group
// if (opt.orphaned_tabs_to_ungrouped === true && document.getElementById(message.tabId) == null && !NewTab.pinned) {
if ( opt . orphaned _tabs _to _ungrouped === true && ! NewTab . pinned ) {
if ( opt . debug ) {
log ( "tab_created: orphan case, orphaned tab goes to ungrouped" ) ;
}
if ( active _group != "tab_list" ) {
SetActiveGroup ( "tab_list" , false , false ) ;
}
}
if ( opt . append _orphan _tab == "after_active" ) {
if ( opt . debug ) {
log ( "tab_created: orphan case, appending tab after active" ) ;
}
AppendTab ( NewTab , false , false , ( document . querySelector ( "#" + active _group + " .active_tab" ) != null ? document . querySelector ( "#" + active _group + " .active_tab" ) . id : undefined ) , ( NewTab . pinned ? true : false ) , false , true , false , false , true , false ) ;
}
if ( opt . append _orphan _tab == "top" ) {
if ( opt . debug ) {
log ( "tab_created: orphan case, appending tab on top" ) ;
}
AppendTab ( NewTab , false , false , false , false , false , true , false , false , true , false ) ;
}
if ( opt . append _orphan _tab == "bottom" || opt . append _orphan _tab == "as_child" ) {
if ( opt . debug ) {
log ( "tab_created: orphan case, appending tab on bottom" ) ;
}
AppendTab ( NewTab , false , false , false , true , false , true , false , false , true , false ) ;
2018-01-01 19:50:56 +01:00
}
2017-11-12 22:00:40 +01:00
}
}
2018-05-22 02:11:29 +02:00
if ( opt . move _tabs _on _url _change === "all_new" ) {
AppendTabToGroupOnRegexMatch ( message . tabId , NewTab . url ) ;
2017-11-12 22:00:40 +01:00
}
2018-05-22 02:11:29 +02:00
if ( NewTab . openerTabId ) { // check if openerTabId is defined, if it's in DOM and if it's closed, then change it to open
let openerTab = document . querySelector ( ".c[id='" + NewTab . openerTabId + "']" ) ;
if ( openerTab != null ) {
openerTab . classList . remove ( "c" ) ;
openerTab . classList . add ( "o" ) ;
}
2017-11-12 22:00:40 +01:00
}
2018-05-22 02:11:29 +02:00
if ( opt . syncro _tabbar _tabs _order ) {
let tabIds = Array . prototype . map . call ( document . querySelectorAll ( ".pin, .tab" ) , function ( s ) {
return parseInt ( s . id ) ;
} ) ;
chrome . tabs . move ( message . tabId , { index : tabIds . indexOf ( message . tabId ) } ) ;
2017-11-12 22:00:40 +01:00
}
2018-05-22 02:11:29 +02:00
RefreshExpandStates ( ) ;
setTimeout ( function ( ) {
schedule _update _data ++ ;
} , 500 ) ;
setTimeout ( function ( ) {
RefreshCounters ( ) ;
RefreshGUI ( ) ;
} , 50 ) ;
2017-11-12 22:00:40 +01:00
}
2018-05-22 02:11:29 +02:00
} ) ;
2018-03-13 14:39:34 +01:00
return ;
}
if ( message . command == "tab_attached" ) {
2018-05-22 02:11:29 +02:00
if ( opt . debug ) {
log ( "chrome event: " + message . command + ", tabId: " + message . tabId + ", tab is pinned: " + message . tab . pinned + ", ParentId: " + message . ParentId ) ;
}
2018-03-13 14:39:34 +01:00
AppendTab ( message . tab , message . ParentId , false , false , true , false , true , false , false , true , false ) ;
RefreshGUI ( ) ;
return ;
}
if ( message . command == "tab_detached" ) {
2018-05-22 02:11:29 +02:00
if ( opt . debug ) {
log ( "chrome event: " + message . command + ", tabId: " + message . tabId ) ;
}
let ctDetachedParent = document . getElementById ( message . tabId ) . childNodes [ 1 ] ;
2018-03-13 14:39:34 +01:00
if ( ctDetachedParent != null ) {
if ( opt . promote _children _in _first _child == true && ctDetachedParent . childNodes . length > 1 ) {
2018-05-22 02:11:29 +02:00
let ctNewParent = document . getElementById ( ctDetachedParent . firstChild . id ) . childNodes [ 1 ] ;
2018-03-13 14:39:34 +01:00
ctDetachedParent . parentNode . parentNode . insertBefore ( ctDetachedParent . firstChild , ctDetachedParent . parentNode ) ;
while ( ctDetachedParent . firstChild ) {
ctNewParent . appendChild ( ctDetachedParent . firstChild ) ;
}
} else {
while ( ctDetachedParent . firstChild ) {
ctDetachedParent . parentNode . parentNode . insertBefore ( ctDetachedParent . firstChild , ctDetachedParent . parentNode ) ;
}
2018-01-01 19:50:56 +01:00
}
2018-03-13 14:39:34 +01:00
}
RemoveTabFromList ( message . tabId ) ;
setTimeout ( function ( ) {
2017-11-12 22:00:40 +01:00
schedule _update _data ++ ;
2018-03-13 14:39:34 +01:00
} , 300 ) ;
RefreshGUI ( ) ;
return ;
}
if ( message . command == "tab_removed" ) {
2018-05-22 02:11:29 +02:00
if ( opt . debug ) {
log ( "chrome event: " + message . command + ", tabId: " + message . tabId ) ;
}
if ( EmptyTabs . indexOf ( message . tabId ) != - 1 ) {
EmptyTabs . splice ( EmptyTabs . indexOf ( message . tabId ) , 1 ) ;
}
2018-03-13 14:39:34 +01:00
let mTab = document . getElementById ( message . tabId ) ;
if ( mTab != null ) {
2018-05-22 02:11:29 +02:00
let ctParent = mTab . childNodes [ 1 ] ;
if ( opt . debug ) {
log ( "tab_removed, promote children: " + opt . promote _children ) ;
}
2018-03-13 14:39:34 +01:00
if ( opt . promote _children == true ) {
if ( opt . promote _children _in _first _child == true && ctParent . childNodes . length > 1 ) {
2018-05-22 02:11:29 +02:00
let ctNewParent = document . getElementById ( ctParent . firstChild . id ) . childNodes [ 1 ] ;
2018-03-13 14:39:34 +01:00
ctParent . parentNode . parentNode . insertBefore ( ctParent . firstChild , ctParent . parentNode ) ;
while ( ctParent . firstChild ) {
ctNewParent . appendChild ( ctParent . firstChild ) ;
2017-11-20 18:14:07 +01:00
}
} else {
2018-03-13 14:39:34 +01:00
while ( ctParent . firstChild ) {
ctParent . parentNode . parentNode . insertBefore ( ctParent . firstChild , ctParent . parentNode ) ;
2017-11-20 18:14:07 +01:00
}
2017-11-12 22:00:40 +01:00
}
2018-03-13 14:39:34 +01:00
} else {
document . querySelectorAll ( "[id='" + message . tabId + "'] .tab" ) . forEach ( function ( s ) {
chrome . tabs . remove ( parseInt ( s . id ) ) ;
} ) ;
2017-11-12 22:00:40 +01:00
}
RemoveTabFromList ( message . tabId ) ;
RefreshExpandStates ( ) ;
2018-01-01 19:50:56 +01:00
setTimeout ( function ( ) {
schedule _update _data ++ ;
} , 300 ) ;
2017-11-12 22:00:40 +01:00
RefreshGUI ( ) ;
2018-01-01 19:50:56 +01:00
RefreshCounters ( ) ;
2018-03-13 14:39:34 +01:00
}
return ;
}
if ( message . command == "tab_activated" ) {
2018-05-22 02:11:29 +02:00
if ( opt . debug ) {
log ( "chrome event: " + message . command + ", tabId: " + message . tabId ) ;
}
SetActiveTab ( message . tabId , true ) ;
2018-03-13 14:39:34 +01:00
return ;
}
if ( message . command == "tab_attention" ) {
2018-05-22 02:11:29 +02:00
if ( opt . debug ) {
log ( "chrome event: " + message . command + ", tabId: " + message . tabId ) ;
}
2018-03-13 14:39:34 +01:00
SetAttentionIcon ( message . tabId ) ;
return ;
}
if ( message . command == "tab_updated" ) {
2018-05-22 02:11:29 +02:00
if ( opt . debug ) {
log ( "chrome event: " + message . command + ", tabId: " + message . tabId + ", changeInfo: " + JSON . stringify ( message . changeInfo ) ) ;
// log(message.changeInfo);
}
2018-03-13 14:39:34 +01:00
if ( message . changeInfo . favIconUrl != undefined || message . changeInfo . url != undefined ) {
2018-01-01 19:50:56 +01:00
setTimeout ( function ( ) {
2018-03-13 14:39:34 +01:00
GetFaviconAndTitle ( message . tabId , true ) ;
} , 100 ) ;
}
if ( message . changeInfo . title != undefined ) {
setTimeout ( function ( ) {
GetFaviconAndTitle ( message . tabId , true ) ;
} , 1000 ) ;
}
if ( message . changeInfo . audible != undefined || message . changeInfo . mutedInfo != undefined ) {
RefreshMediaIcon ( message . tabId ) ;
}
if ( message . changeInfo . discarded != undefined ) {
RefreshDiscarded ( message . tabId ) ;
}
if ( message . changeInfo . pinned != undefined ) {
let updateTab = document . getElementById ( message . tabId ) ;
2018-05-22 02:11:29 +02:00
if ( updateTab != null ) {
if ( message . tab . pinned && updateTab . classList . contains ( "pin" ) == false ) {
SetTabClass ( message . tabId , true ) ;
schedule _update _data ++ ;
}
if ( ! message . tab . pinned && updateTab . classList . contains ( "tab" ) == false ) {
SetTabClass ( message . tabId , false ) ;
schedule _update _data ++ ;
}
2017-11-12 22:00:40 +01:00
}
2018-03-13 14:39:34 +01:00
RefreshExpandStates ( ) ;
}
2018-05-22 02:11:29 +02:00
// if set to append when url changes and matches pre-set group
if ( message . changeInfo . url != undefined && message . changeInfo . url != newTabUrl ) {
// if (((opt.move_tabs_on_url_change == "from_empty" || opt.move_tabs_on_url_change == "from_empty_b") && EmptyTabs.indexOf(message.tabId) != -1) || opt.move_tabs_on_url_change == "always") {
if ( EmptyTabs . indexOf ( message . tabId ) != - 1 || opt . move _tabs _on _url _change == "always" ) {
AppendTabToGroupOnRegexMatch ( message . tabId , message . changeInfo . url ) ;
}
if ( EmptyTabs . indexOf ( message . tabId ) != - 1 ) {
EmptyTabs . splice ( EmptyTabs . indexOf ( message . tabId ) , 1 ) ;
}
}
2018-03-13 14:39:34 +01:00
return ;
}
if ( message . command == "remote_update" ) {
2018-05-22 02:11:29 +02:00
if ( opt . debug ) {
log ( "chrome event: " + message . command + ", tabId: " + message . tabId ) ;
log ( message ) ;
}
2018-03-13 14:39:34 +01:00
RearrangeTreeStructure ( message . groups , message . folders , message . tabs ) ;
sendResponse ( true ) ;
return ;
2017-11-12 22:00:40 +01:00
}
}
} ) ;
}