Created
November 19, 2017 21:40
-
-
Save Pimm/dbafbd12011afb0c09086fa4f930c2f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Converts a Tab Groups backup to HTML. | |
*/ | |
function convertTabGroupsBackupToHtml(backup) { | |
if (undefined === backup.version | |
|| 'tabGroups' != backup.version[0] || 1 != backup.version[1]) { | |
throw new Error('Unexpected input'); | |
} | |
return [ | |
'<ul>', | |
backup.windows.map(window => { | |
// Create a map which maps group identifiers to their tabs. | |
const groupIdentifierTabsMap = {}; | |
window.tabs.forEach(tab => { | |
// Determine the group this tab is in. | |
var groupIdentifier /* = undefined */; | |
if (undefined !== tab.extData['tabview-tab']) { | |
try { | |
groupIdentifier = JSON.parse(tab.extData['tabview-tab'])['groupID']; | |
} catch (error) { | |
// In case the data chunk is not valid JSON (or that JSON does not represent an object or something) continue | |
// as if there was no group identifier. | |
} | |
} | |
if (undefined === groupIdentifier) { | |
groupIdentifier = 'none'; | |
} | |
// Add the tab to the map. | |
if (undefined === groupIdentifierTabsMap[groupIdentifier]) { | |
groupIdentifierTabsMap[groupIdentifier] = [tab]; | |
} else /* if (undefined !== groupIdentifierTabsMap[groupIdentifier]) */ { | |
groupIdentifierTabsMap[groupIdentifier].push(tab); | |
} | |
}); | |
// Parse the map which maps group identifiers to meta data about that group. | |
var groupIdentifierMetaDataMap; | |
if (undefined !== window.extData['tabview-group']) { | |
try { | |
groupIdentifierMetaDataMap = JSON.parse(window.extData['tabview-group']); | |
} catch (error) { | |
// In case the data chunk is not valid JSON, continue as if there was no data. | |
} | |
} | |
if (undefined === groupIdentifierMetaDataMap) { | |
groupIdentifierMetaDataMap = {}; | |
} | |
return [ | |
'<li>', | |
'<ul>', | |
Object.keys(groupIdentifierTabsMap).map(groupIdentifier => { | |
// Fetch the title. | |
var title; | |
if (undefined !== groupIdentifierMetaDataMap[groupIdentifier]) { | |
title = groupIdentifierMetaDataMap[groupIdentifier]['title']; | |
} | |
if (undefined === title | |
|| 0 == title.length) { | |
title = groupIdentifier; | |
} | |
return [ | |
'<li>', | |
['<h4>', title, '</h4>'].join(''), | |
'<ul>', | |
groupIdentifierTabsMap[groupIdentifier].map(tab => { | |
return [ | |
'<li>', | |
['<a href="', tab.entries[0].url, '">', tab.entries[0].title, '</a>'].join(''), | |
'</li>' | |
].join('\n'); | |
}).join('\n'), | |
'</ul>', | |
'</li>' | |
].join('\n'); | |
}).join('\n'), | |
'</ul>', | |
'</li>' | |
].join('\n'); | |
}).join('\n'), | |
'</ul>' | |
].join('\n'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment