2018-07-03 20:36:38 +02: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/
////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////// START BACKGROUND SCRIPT /////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
document . addEventListener ( "DOMContentLoaded" , function ( ) {
if ( browserId == "F" ) {
setTimeout ( function ( ) {
StartBackgroundListeners ( ) ;
QuantumStart ( 0 ) ;
} , 500 ) ;
} else {
StartBackgroundListeners ( ) ;
ChromiumLoadTabs ( 0 ) ;
}
} ) ;
////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////// BACKGROUND FUNCTIONS /////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
function pushlog ( log ) {
b . debug . push ( log ) ;
if ( b . debug . length > 100 ) {
b . debug . splice ( 0 , 1 ) ;
}
console . log ( log ) ;
b . schedule _save ++ ;
}
function ReplaceParents ( oldTabId , newTabId ) {
for ( let tabId in b . tabs ) {
if ( b . tabs [ tabId ] . parent == oldTabId ) {
b . tabs [ tabId ] . parent = newTabId ;
}
}
}
async function DiscardTab ( tabId ) {
let DiscardTimeout = 0 ;
let Discard = setInterval ( function ( ) {
chrome . tabs . get ( tabId , function ( tab ) {
if ( ( tab . favIconUrl != undefined && tab . favIconUrl != "" && tab . title != undefined && tab . title != "" ) || tab . status == "complete" || tab . audible ) {
chrome . tabs . discard ( tab . id ) ;
clearInterval ( Discard ) ;
}
if ( DiscardTimeout > 300 ) {
clearInterval ( Discard ) ;
}
} ) ;
DiscardTimeout ++ ;
} , 2000 ) ;
}
async function DiscardWindow ( windowId ) {
let DiscardTimeout = 0 ;
let DiscardedTabs = 0 ;
let Discard = setInterval ( function ( ) {
chrome . windows . get ( windowId , { populate : true } , function ( w ) {
for ( let i = 0 ; i < w . tabs . length ; i ++ ) {
if ( w . tabs [ i ] . discarded == false && w . tabs [ i ] . active == false ) {
if ( ( w . tabs [ i ] . favIconUrl != undefined && w . tabs [ i ] . favIconUrl != "" && w . tabs [ i ] . title != undefined && w . tabs [ i ] . title != "" ) || w . tabs [ i ] . status == "complete" || tab . audible ) {
chrome . tabs . discard ( w . tabs [ i ] . id ) ;
DiscardedTabs ++ ;
}
}
}
if ( DiscardedTabs == w . tabs . length ) {
clearInterval ( Discard ) ;
}
} ) ;
if ( DiscardTimeout > 300 ) {
clearInterval ( Discard ) ;
}
DiscardTimeout ++ ;
} , 5000 ) ;
}
function GetTabGroupId ( tabId , windowId ) {
let groupId = "tab_list" ;
if ( tabId == undefined || windowId == undefined || b . windows [ windowId ] == undefined || b . tabs [ tabId ] == undefined ) {
return groupId ;
}
let parent = b . tabs [ tabId ] . parent ;
while ( parent ) {
if ( isNaN ( parent ) == false && b . tabs [ parent ] ) {
parent = b . tabs [ parent ] . parent ;
} else {
if ( parent . match ( "tab_list|g_|f_" ) == null && b . tabs [ parent ] ) {
parent = b . tabs [ parent ] . parent ;
} else {
if ( parent . match ( "f_" ) != null && b . windows [ windowId ] . folders [ parent ] ) {
parent = b . windows [ windowId ] . folders [ parent ] . parent ;
} else {
if ( parent . match ( "pin_list|tab_list|g_" ) != null ) {
groupId = parent ;
parent = false ;
} else {
parent = false ;
}
}
}
}
}
return groupId ;
}
function GetTabParents ( tabId ) {
let Parents = [ ] ;
if ( tabId == undefined ) {
return Parents ;
}
if ( b . tabs [ tabId ] == undefined ) {
return Parents ;
}
while ( b . tabs [ tabId ] . parent != "" && b . tabs [ b . tabs [ tabId ] . parent ] != undefined ) {
if ( b . tabs [ b . tabs [ tabId ] . parent ] ) {
Parents . push ( b . tabs [ tabId ] . parent ) ;
}
tabId = b . tabs [ tabId ] . parent ;
}
return Parents ;
}
function GetChildren ( parentId ) {
let Children = [ ] ;
for ( let tId in b . tabs ) {
if ( b . tabs [ tId ] . parent == parentId ) {
Children . push ( parseInt ( tId ) ) ;
}
}
for ( let i = 0 ; i < Children . length - 1 ; i ++ ) {
for ( let j = i + 1 ; j < Children . length ; j ++ ) {
if ( b . tabs [ Children [ i ] ] . index > b . tabs [ Children [ j ] ] . index ) {
let swap = Children [ i ] ;
Children [ i ] = Children [ j ] ;
Children [ j ] = swap ;
}
}
}
return Children ;
}
function AppendTabToGroupOnRegexMatch ( tabId , windowId , url ) {
let TabGroupId = GetTabGroupId ( tabId , windowId ) ;
for ( let i = 0 ; i < opt . tab _group _regexes . length ; i ++ ) {
let regexPair = opt . tab _group _regexes [ i ] ;
if ( url . match ( regexPair [ 0 ] ) ) {
let groupId = FindGroupIdByName ( regexPair [ 1 ] , b . windows [ windowId ] . groups ) ;
let groupName = regexPair [ 1 ] ;
if ( groupId === null ) { // no group
let newGroupID = "" ;
while ( newGroupID == "" ) {
newGroupID = "g_" + GenerateRandomID ( ) ;
for ( let wId in b . windows ) {
for ( let gId in b . windows [ wId ] . groups ) {
console . log ( "check if group id exists" ) ;
if ( gId == newGroupID ) {
newGroupID = "" ;
console . log ( "yup, redo" ) ;
}
}
}
}
groupId = newGroupID ;
b . windows [ windowId ] . groups [ groupId ] = { id : groupId , index : 999 , active _tab : 0 , prev _active _tab : 0 , active _tab _ttid : "" , name : groupName , font : "" } ;
chrome . runtime . sendMessage ( { command : "append_group" , groupId : groupId , group _name : groupName , font _color : "" , windowId : windowId } ) ;
}
if ( TabGroupId != groupId && groupId != null ) {
b . tabs [ tabId ] . parent = groupId ;
setTimeout ( function ( ) {
chrome . runtime . sendMessage ( { command : "append_tab_to_group" , tabId : tabId , groupId : groupId , windowId : windowId } ) ;
} , 100 ) ;
}
break ;
}
}
return b . tabs [ tabId ] . parent ;
}
function FindGroupIdByName ( name , groups ) {
for ( let groupId in groups ) {
if ( ! groups . hasOwnProperty ( groupId ) ) {
continue ;
}
if ( groups [ groupId ] . name === name ) {
return groupId ;
}
}
return null ;
}
////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// QUANTUM //////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
function QuantumStart ( retry ) {
chrome . windows . getAll ( { windowTypes : [ "normal" ] , populate : true } , function ( w ) {
if ( w [ 0 ] . tabs . length == 1 && ( w [ 0 ] . tabs [ 0 ] . url == "about:blank" || w [ 0 ] . tabs [ 0 ] . url == "about:sessionrestore" ) ) {
setTimeout ( function ( ) {
QuantumStart ( retry + 1 ) ;
} , 2000 ) ;
} else {
QuantumLoadTabs ( 0 ) ;
if ( retry > 0 ) {
chrome . runtime . sendMessage ( { command : "reload_sidebar" } ) ;
}
setTimeout ( function ( ) {
b . schedule _save = 0 ;
} , 2000 ) ;
}
} ) ;
}
function QuantumLoadTabs ( retry ) {
chrome . windows . getAll ( { windowTypes : [ "normal" ] , populate : true } , function ( w ) {
chrome . storage . local . get ( null , function ( storage ) {
// LOAD PREFERENCES
GetCurrentPreferences ( storage ) ;
// CACHED COUNTS AND STUFF
// let b.tt_ids = {};
let tabs _matched = 0 ;
let tabs _count = 0 ;
for ( let wIndex = 0 ; wIndex < w . length ; wIndex ++ ) {
tabs _count += w [ wIndex ] . tabs . length ;
}
let lastWinId = w [ w . length - 1 ] . id ;
let lastTabId = w [ w . length - 1 ] . tabs [ w [ w . length - 1 ] . tabs . length - 1 ] . id ;
let WinCount = w . length ;
if ( opt . debug == true ) {
if ( storage . debug _log != undefined ) {
b . debug = storage . debug _log ;
}
if ( retry == 0 ) {
pushlog ( "TreeTabs background start" ) ;
}
}
for ( let wIndex = 0 ; wIndex < WinCount ; wIndex ++ ) {
let winIndex = wIndex ;
let winId = w [ winIndex ] . id ;
let tabsCount = w [ winIndex ] . tabs . length ;
// LOAD TTID FROM FIREFOX GET WINDOW VALUE
let win = Promise . resolve ( browser . sessions . getWindowValue ( winId , "TTdata" ) ) . then ( function ( WindowData ) {
if ( opt . skip _load == false && WindowData != undefined ) {
b . windows [ winId ] = Object . assign ( { } , WindowData ) ;
} else {
QuantumAppendWinTTId ( winId ) ;
}
2018-07-04 17:29:50 +02:00
2018-07-09 00:16:44 +02:00
b . windows [ winId ] . activeTabId = 0 ; // legacy
2018-07-04 17:29:50 +02:00
2018-07-03 20:36:38 +02:00
for ( let tIndex = 0 ; tIndex < tabsCount ; tIndex ++ ) {
let tab = w [ winIndex ] . tabs [ tIndex ] ;
let tabIndex = tIndex ;
let tabId = w [ winIndex ] . tabs [ tabIndex ] . id ;
let tabPinned = w [ winIndex ] . tabs [ tabIndex ] . pinned ;
if ( tab . active ) {
2018-07-09 00:16:44 +02:00
b . windows [ winId ] . activeTabId = tabId ;
2018-07-03 20:36:38 +02:00
}
// LOAD TTID FROM FIREFOX GET TAB VALUE
let tt _tab = Promise . resolve ( browser . sessions . getTabValue ( tabId , "TTdata" ) ) . then ( function ( TabData ) {
if ( opt . skip _load == false && TabData != undefined ) {
b . tabs [ tabId ] = Object . assign ( { } , TabData ) ;
b . tt _ids [ b . tabs [ tabId ] . ttid ] = tabId ;
tabs _matched ++ ;
} else {
QuantumAppendTabTTId ( tab ) ;
}
// IF ON LAST TAB AND LAST WINDOW, START MATCHING LOADED DATA
if ( tabId == lastTabId && winId == lastWinId ) {
// OK, DONE, NOW REPLACE OLD PARENTS IDS WITH THIS SESSION IDS
for ( let ThisSessonTabId in b . tabs ) {
if ( b . tabs [ ThisSessonTabId ] . parent _ttid != "" && b . tt _ids [ b . tabs [ ThisSessonTabId ] . parent _ttid ] != undefined ) {
b . tabs [ ThisSessonTabId ] . parent = b . tt _ids [ b . tabs [ ThisSessonTabId ] . parent _ttid ] ;
}
}
// OK, SAME THING FOR ACTIVE TABS IN GROUPS
for ( let ThisSessonWinId in b . windows ) {
for ( let group in b . windows [ ThisSessonWinId ] . groups ) {
if ( b . tt _ids [ b . windows [ ThisSessonWinId ] . groups [ group ] . active _tab _ttid ] != undefined ) {
b . windows [ ThisSessonWinId ] . groups [ group ] . active _tab = b . tt _ids [ b . windows [ ThisSessonWinId ] . groups [ group ] . active _tab _ttid ] ;
}
if ( b . tt _ids [ b . windows [ ThisSessonWinId ] . groups [ group ] . prev _active _tab _ttid ] != undefined ) {
b . windows [ ThisSessonWinId ] . groups [ group ] . prev _active _tab = b . tt _ids [ b . windows [ ThisSessonWinId ] . groups [ group ] . prev _active _tab _ttid ] ;
}
}
}
if ( opt . debug ) { pushlog ( "QuantumLoadTabs, retry: " + retry ) ; pushlog ( "Current windows count is: " + w . length ) ; pushlog ( "Current tabs count is: " + tabs _count ) ; pushlog ( "Matching tabs: " + tabs _matched ) ; pushlog ( "Current windows:" ) ; pushlog ( w ) ; }
// will try to find tabs for 3 times
if ( opt . skip _load == true || retry > 2 || ( tabs _matched > tabs _count * 0.5 ) ) {
b . running = true ;
QuantumAutoSaveData ( ) ;
QuantumStartListeners ( ) ;
delete DefaultToolbar ; delete DefaultTheme ; delete DefaultPreferences ;
} else {
if ( opt . debug ) {
pushlog ( "Attempt " + retry + " failed, matched tabs was below 50%" ) ;
}
setTimeout ( function ( ) {
QuantumLoadTabs ( retry + 1 ) ;
} , 2000 ) ;
}
}
} ) ;
}
} ) ;
}
} ) ;
} ) ;
}
// save every second if there is anything to save obviously
async function QuantumAutoSaveData ( ) {
setInterval ( function ( ) {
if ( b . schedule _save > 1 ) {
b . schedule _save = 1 ;
}
if ( b . running && b . schedule _save > 0 && Object . keys ( b . tabs ) . length > 1 ) {
chrome . windows . getAll ( { windowTypes : [ 'normal' ] , populate : true } , function ( w ) {
let WinCount = w . length ;
for ( let wIndex = 0 ; wIndex < WinCount ; wIndex ++ ) {
let winId = w [ wIndex ] . id ;
if ( b . windows [ winId ] != undefined && b . windows [ winId ] . ttid != undefined && b . windows [ winId ] . group _bar != undefined && b . windows [ winId ] . search _filter != undefined && b . windows [ winId ] . active _shelf != undefined && b . windows [ winId ] . active _group != undefined && b . windows [ winId ] . groups != undefined && b . windows [ winId ] . folders != undefined ) {
browser . sessions . setWindowValue ( winId , "TTdata" , b . windows [ winId ] ) ;
}
let TabsCount = w [ wIndex ] . tabs . length ;
for ( let tabIndex = 0 ; tabIndex < TabsCount ; tabIndex ++ ) {
let tabId = w [ wIndex ] . tabs [ tabIndex ] . id ;
if ( b . tabs [ tabId ] != undefined && b . tabs [ tabId ] . ttid != undefined && b . tabs [ tabId ] . parent != undefined && b . tabs [ tabId ] . index != undefined && b . tabs [ tabId ] . expand != undefined ) {
browser . sessions . setTabValue ( tabId , "TTdata" , b . tabs [ tabId ] ) ;
}
}
}
b . schedule _save -- ;
} ) ;
}
if ( opt . debug == true ) { chrome . storage . local . set ( { debug _log : b . debug } ) ; }
} , 1000 ) ;
}
function QuantumGenerateNewWindowID ( ) {
let newID = "" ;
while ( newID == "" ) {
newID = "w_" + GenerateRandomID ( ) ;
for ( let wId in b . windows ) {
if ( wId == newID ) {
newID = "" ;
}
}
}
return newID ;
}
function QuantumGenerateNewTabID ( ) {
let newID = "" ;
while ( newID == "" ) {
newID = "t_" + GenerateRandomID ( ) ;
for ( let tId in b . tabs ) {
if ( tId == newID ) {
newID = "" ;
}
}
}
return newID ;
}
function QuantumAppendTabTTId ( tab ) {
let NewTTTabId = QuantumGenerateNewTabID ( ) ;
if ( b . tabs [ tab . id ] != undefined ) {
b . tabs [ tab . id ] . ttid = NewTTTabId ;
} else {
b . tabs [ tab . id ] = { ttid : NewTTTabId , parent : ( b . windows [ tab . windowId ] ? b . windows [ tab . windowId ] . active _group : "tab_list" ) , parent _ttid : "" , index : tab . index , expand : "" } ;
}
b . tt _ids [ NewTTTabId ] = tab . id ;
return NewTTTabId ;
// if (b.schedule_save > 0) browser.sessions.setTabValue( tab.id, "TTdata", b.tabs[tab.id] );
}
function QuantumAppendWinTTId ( windowId ) {
let NewTTWindowId = QuantumGenerateNewWindowID ( ) ;
if ( b . windows [ windowId ] != undefined ) {
b . windows [ windowId ] . ttid = NewTTWindowId ;
} else {
2018-07-09 00:16:44 +02:00
b . windows [ windowId ] = { activeTabId : 0 , ttid : NewTTWindowId , group _bar : opt . groups _toolbar _default , search _filter : "url" , active _shelf : "" , active _group : "tab_list" , groups : { tab _list : { id : "tab_list" , index : 0 , active _tab : 0 , active _tab _ttid : "" , prev _active _tab : 0 , prev _active _tab _ttid : "" , name : labels . ungrouped _group , font : "" } } , folders : { } } ;
2018-07-03 20:36:38 +02:00
}
// if (b.schedule_save > 0) browser.sessions.setWindowValue( windowId, "TTdata", b.windows[windowId] );
}
////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// CHROMIUM /////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
function ChromiumLoadTabs ( retry ) {
chrome . windows . getAll ( { windowTypes : [ 'normal' ] , populate : true } , function ( w ) {
chrome . storage . local . get ( null , function ( storage ) {
// LOAD PREFERENCES
GetCurrentPreferences ( storage ) ;
// load tabs and windows from storage
let refTabs = { } ;
let tabs _matched = 0 ;
let w _count = storage . w _count ? storage . w _count : 0 ;
let t _count = storage . t _count ? storage . t _count : 0 ;
let LoadedWindows = storage . windows ? storage . windows : [ ] ;
let LoadedTabs = storage . tabs ? storage . tabs : [ ] ;
let CurrentTabsCount = 0 ;
for ( let wIndex = 0 ; wIndex < w . length ; wIndex ++ ) {
CurrentTabsCount += w [ wIndex ] . tabs . length ;
}
let bak = ( 1 + retry ) <= 3 ? ( 1 + retry ) : 3 ;
if ( opt . skip _load == false ) {
// if loaded tabs mismatch by 50%, then try to load back
if ( LoadedTabs . length < t _count * 0.5 ) {
LoadedTabs = storage [ "tabs_BAK" + bak ] ? storage [ "tabs_BAK" + bak ] : [ ] ;
}
// if loaded windows mismatch, then try to load back
if ( LoadedWindows . length < w _count ) {
LoadedWindows = storage [ "windows_BAK" + bak ] ? storage [ "windows_BAK" + bak ] : [ ] ;
}
} else {
tabs _matched = CurrentTabsCount ;
}
if ( opt . debug == true ) {
if ( storage . debug _log != undefined ) {
b . debug = storage . debug _log ;
}
if ( retry == 0 ) {
pushlog ( "TreeTabs background start" ) ;
}
}
// CACHED COUNTS
let WinCount = w . length ;
let LoadedWinCount = LoadedWindows . length ;
let LoadedTabsCount = LoadedTabs . length ;
for ( let wIndex = 0 ; wIndex < WinCount ; wIndex ++ ) {
if ( w [ wIndex ] . tabs [ 0 ] . url != "chrome://videopopout/" ) { // this is for opera for their extra video popup, which is weirdly queried as a "normal" window
let winId = w [ wIndex ] . id ;
let url1 = w [ wIndex ] . tabs [ 0 ] . url ;
let url2 = w [ wIndex ] . tabs [ w [ wIndex ] . tabs . length - 1 ] . url ;
ChromiumAddWindowData ( winId ) ;
if ( opt . skip _load == false ) {
for ( let LwIndex = 0 ; LwIndex < LoadedWinCount ; LwIndex ++ ) {
if ( LoadedWindows [ LwIndex ] . url1 == url1 || LoadedWindows [ LwIndex ] . url2 == url2 ) {
if ( LoadedWindows [ LwIndex ] . group _bar ) { b . windows [ winId ] . group _bar = LoadedWindows [ LwIndex ] . group _bar ; }
if ( LoadedWindows [ LwIndex ] . search _filter ) { b . windows [ winId ] . search _filter = LoadedWindows [ LwIndex ] . search _filter ; }
if ( LoadedWindows [ LwIndex ] . active _shelf ) { b . windows [ winId ] . active _shelf = LoadedWindows [ LwIndex ] . active _shelf ; }
if ( LoadedWindows [ LwIndex ] . active _group ) { b . windows [ winId ] . active _group = LoadedWindows [ LwIndex ] . active _group ; }
if ( Object . keys ( LoadedWindows [ LwIndex ] . groups ) . length > 0 ) { b . windows [ winId ] . groups = Object . assign ( { } , LoadedWindows [ LwIndex ] . groups ) ; }
if ( Object . keys ( LoadedWindows [ LwIndex ] . folders ) . length > 0 ) { b . windows [ winId ] . folders = Object . assign ( { } , LoadedWindows [ LwIndex ] . folders ) ; }
LoadedWindows [ LwIndex ] . url1 = "" ;
LoadedWindows [ LwIndex ] . url2 = "" ;
break ;
}
}
}
}
}
// add new hashes for current tabs
for ( let wIndex = 0 ; wIndex < WinCount ; wIndex ++ ) {
let TabsCount = w [ wIndex ] . tabs . length ;
for ( let tabIndex = 0 ; tabIndex < TabsCount ; tabIndex ++ ) {
ChromiumHashURL ( w [ wIndex ] . tabs [ tabIndex ] ) ;
if ( w [ wIndex ] . tabs [ tabIndex ] . active ) {
2018-07-09 00:16:44 +02:00
b . windows [ w [ wIndex ] . id ] . activeTabId = w [ wIndex ] . tabs [ tabIndex ] . id ;
2018-07-03 20:36:38 +02:00
}
}
}
// compare saved tabs from storage to current session tabs, but can be skipped if set in options
if ( opt . skip _load == false && LoadedTabs . length > 0 ) {
for ( let wIndex = 0 ; wIndex < WinCount ; wIndex ++ ) {
let TabsCount = w [ wIndex ] . tabs . length ;
for ( let tabIndex = 0 ; tabIndex < TabsCount ; tabIndex ++ ) {
for ( let LtabIndex = 0 ; LtabIndex < LoadedTabsCount ; LtabIndex ++ ) {
let tabId = w [ wIndex ] . tabs [ tabIndex ] . id ;
if ( LoadedTabs [ LtabIndex ] . hash == b . tabs [ tabId ] . hash && refTabs [ LoadedTabs [ LtabIndex ] . id ] == undefined ) {
refTabs [ LoadedTabs [ LtabIndex ] . id ] = tabId ;
if ( LoadedTabs [ LtabIndex ] . parent ) { b . tabs [ tabId ] . parent = LoadedTabs [ LtabIndex ] . parent ; }
if ( LoadedTabs [ LtabIndex ] . index ) { b . tabs [ tabId ] . index = LoadedTabs [ LtabIndex ] . index ; }
if ( LoadedTabs [ LtabIndex ] . expand ) { b . tabs [ tabId ] . expand = LoadedTabs [ LtabIndex ] . expand ; }
LoadedTabs [ LtabIndex ] . hash = undefined ;
tabs _matched ++ ;
break ;
}
}
}
}
// replace parents tabIds for new ones, for that purpose refTabs was made before
for ( let tabId in b . tabs ) {
if ( refTabs [ b . tabs [ tabId ] . parent ] != undefined ) {
b . tabs [ tabId ] . parent = refTabs [ b . tabs [ tabId ] . parent ] ;
}
}
// replace active tab ids for each group using refTabs
for ( let windowId in b . windows ) {
for ( let group in b . windows [ windowId ] . groups ) {
if ( refTabs [ b . windows [ windowId ] . groups [ group ] . active _tab ] ) {
b . windows [ windowId ] . groups [ group ] . active _tab = refTabs [ b . windows [ windowId ] . groups [ group ] . active _tab ] ;
}
if ( refTabs [ b . windows [ windowId ] . groups [ group ] . prev _active _tab ] ) {
b . windows [ windowId ] . groups [ group ] . prev _active _tab = refTabs [ b . windows [ windowId ] . groups [ group ] . prev _active _tab ] ;
}
}
}
}
if ( opt . debug ) {
pushlog ( "ChromiumLoadTabs, retry: " + retry ) ; pushlog ( "Current windows count is: " + w . length ) ; pushlog ( "Saved windows count is: " + LoadedWindows . length ) ; pushlog ( "Current tabs count is: " + CurrentTabsCount ) ;
pushlog ( "Loaded tabs count is: " + LoadedTabsCount ) ; pushlog ( "Matching tabs: " + tabs _matched ) ; pushlog ( "Current windows:" ) ; pushlog ( w ) ;
}
// will loop trying to find tabs
if ( opt . skip _load || retry >= 5 || ( tabs _matched > t _count * 0.5 ) ) {
b . running = true ;
ChromiumAutoSaveData ( 0 , 1000 ) ; ChromiumAutoSaveData ( 1 , 300000 ) ; ChromiumAutoSaveData ( 2 , 600000 ) ; ChromiumAutoSaveData ( 3 , 1800000 ) ;
ChromiumStartListeners ( ) ;
delete DefaultToolbar ; delete DefaultTheme ; delete DefaultPreferences ;
b . schedule _save = - 1 ; // 2 operations must be made to start saving data
} else {
if ( opt . debug ) {
pushlog ( "Attempt " + retry + " failed, matched tabs was below 50%" ) ;
}
setTimeout ( function ( ) {
ChromiumLoadTabs ( retry + 1 ) ;
} , 5000 ) ;
}
} ) ;
} ) ;
}
async function ChromiumAutoSaveData ( BAK , LoopTimer ) {
setInterval ( function ( ) {
if ( b . schedule _save > 1 || BAK > 0 ) {
b . schedule _save = 1 ;
}
if ( b . running && b . schedule _save > 0 && Object . keys ( b . tabs ) . length > 1 ) {
chrome . windows . getAll ( { windowTypes : [ 'normal' ] , populate : true } , function ( w ) {
let WinCount = w . length ;
let t _count = 0 ;
let counter = 0 ;
let Windows = [ ] ;
let Tabs = [ ] ;
for ( let wIndex = 0 ; wIndex < WinCount ; wIndex ++ ) {
t _count += w [ wIndex ] . tabs . length ;
}
for ( let wIndex = 0 ; wIndex < WinCount ; wIndex ++ ) {
let winId = w [ wIndex ] . id ;
if ( b . windows [ winId ] != undefined && b . windows [ winId ] . group _bar != undefined && b . windows [ winId ] . search _filter != undefined && b . windows [ winId ] . active _shelf != undefined && b . windows [ winId ] . active _group != undefined && b . windows [ winId ] . groups != undefined && b . windows [ winId ] . folders != undefined ) {
Windows . push ( { url1 : w [ wIndex ] . tabs [ 0 ] . url , url2 : w [ wIndex ] . tabs [ w [ wIndex ] . tabs . length - 1 ] . url , group _bar : b . windows [ winId ] . group _bar , search _filter : b . windows [ winId ] . search _filter , active _shelf : b . windows [ winId ] . active _shelf , active _group : b . windows [ winId ] . active _group , groups : b . windows [ winId ] . groups , folders : b . windows [ winId ] . folders } ) ;
}
let TabsCount = w [ wIndex ] . tabs . length ;
for ( let tabIndex = 0 ; tabIndex < TabsCount ; tabIndex ++ ) {
let tabId = w [ wIndex ] . tabs [ tabIndex ] . id ;
if ( b . tabs [ tabId ] != undefined && b . tabs [ tabId ] . hash != undefined && b . tabs [ tabId ] . parent != undefined && b . tabs [ tabId ] . index != undefined && b . tabs [ tabId ] . expand != undefined ) {
Tabs . push ( { id : tabId , hash : b . tabs [ tabId ] . hash , parent : b . tabs [ tabId ] . parent , index : b . tabs [ tabId ] . index , expand : b . tabs [ tabId ] . expand } ) ;
counter ++ ;
}
}
if ( counter == t _count ) {
chrome . storage . local . set ( { t _count : t _count } ) ;
chrome . storage . local . set ( { w _count : WinCount } ) ;
if ( BAK == 0 ) { chrome . storage . local . set ( { windows : Windows } ) ; chrome . storage . local . set ( { tabs : Tabs } ) ; }
if ( BAK == 1 ) { chrome . storage . local . set ( { windows _BAK1 : Windows } ) ; chrome . storage . local . set ( { tabs _BAK1 : Tabs } ) ; chrome . runtime . sendMessage ( { command : "backup_available" , bak : 1 } ) ; }
if ( BAK == 2 ) { chrome . storage . local . set ( { windows _BAK2 : Windows } ) ; chrome . storage . local . set ( { tabs _BAK2 : Tabs } ) ; chrome . runtime . sendMessage ( { command : "backup_available" , bak : 2 } ) ; }
if ( BAK == 3 ) { chrome . storage . local . set ( { windows _BAK3 : Windows } ) ; chrome . storage . local . set ( { tabs _BAK3 : Tabs } ) ; chrome . runtime . sendMessage ( { command : "backup_available" , bak : 3 } ) ; }
}
}
b . schedule _save -- ;
} ) ;
}
if ( opt . debug == true ) { chrome . storage . local . set ( { debug _log : b . debug } ) ; }
} , LoopTimer ) ;
}
function ChromiumAddWindowData ( winId ) {
2018-07-09 00:16:44 +02:00
b . windows [ winId ] = { activeTabId : 0 , group _bar : opt . groups _toolbar _default , search _filter : "url" , active _shelf : "" , active _group : "tab_list" , groups : { tab _list : { id : "tab_list" , index : 0 , active _tab : 0 , prev _active _tab : 0 , name : labels . ungrouped _group , font : "" } } , folders : { } } ;
2018-07-03 20:36:38 +02:00
}
function ChromiumHashURL ( tab ) {
if ( b . tabs [ tab . id ] == undefined ) { b . tabs [ tab . id ] = { hash : 0 , parent : tab . pinned ? "pin_list" : ( b . windows [ tab . windowId ] ? b . windows [ tab . windowId ] . active _group : "tab_list" ) , index : ( Object . keys ( b . tabs ) . length + 1 ) , expand : "n" } ; }
let hash = 0 ;
for ( let charIndex = 0 ; charIndex < tab . url . length ; charIndex ++ ) {
hash += tab . url . charCodeAt ( charIndex ) ;
}
b . tabs [ tab . id ] . hash = hash ;
}