-
-
Save bdno86/d96f1ad76673dcb0d3d30bcd0cd315b1 to your computer and use it in GitHub Desktop.
Simple Raw Text Export for Google Chrome Bookmarks
This file contains hidden or 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
/** | |
* @function | |
* @description Gets all Bookmarks from Chrome Bookmark repository | |
* @param object window | |
* @return string | |
*/ | |
;(function (window) { | |
try { | |
var bookmarks, output; | |
if (window.chrome === void 0 || window.chrome.bookmarks === void 0) { | |
throw new Error("Chrome Object is not available."); | |
} | |
chrome.bookmarks.getTree(fetchFromTree); | |
window.copy(global.bookmarks); | |
} catch (e) { | |
console.log(e); | |
} | |
/** | |
* @function | |
* @description | |
* @param object tree | |
* @return undefined | |
*/ | |
function fetchFromTree(tree) { | |
var url = document.createElement('a'); | |
var urls = {}; | |
var count = 0; | |
var subTrees = tree[0].children; | |
var crawler = function getBookmarks(subTree) { | |
if (subTree.children) { | |
return getBookmarks(subTree.children); | |
} else { | |
subTree.forEach( | |
function (item) { | |
if (!item.url) { | |
return getBookmarks(item); | |
} else { | |
url.href = item.url; | |
var domain = url.protocol + "//" + url.host; | |
if (urls[domain]) { | |
if (urls[domain].indexOf(item.url) === -1) { | |
urls[domain].push(item.url); | |
} | |
} else { | |
urls[domain] = [item.url]; | |
} | |
//console.log( item.url ); | |
count++; | |
} | |
} | |
); | |
} | |
}; | |
subTrees.forEach(crawler); | |
global.bookmarks = getSQLiteInsertRows(urls); | |
//console.log(count); | |
//console.log(urls); | |
//console.log(getSQLiteInsertRows(urls)); | |
} | |
//@ToDo Export URLs to SQL(ite) INSERTs | |
function getSQLiteInsertRows(tree) { | |
var transactionStr = ''; | |
for (item in tree) { | |
tree[item].forEach( | |
function (item) { | |
//@ToDo escape double-quotes "" | |
transactionStr += 'INSERT OR FAIL INTO bookmark (dateAdded,url) VALUES( (SELECT strftime(\'%s\',\'now\')),'+'\"'+item.replace(/"/g,'\\"')+'\"'+'); \n'; | |
} | |
); | |
} | |
return transactionStr; | |
} | |
function getCSV(bookmarks) { | |
bookmarks.forEach( | |
function (bookmark) { | |
output += '("'+bookmark.url.replace(/"/g,'\"')+'"),\n'; | |
} | |
); | |
return output.replace(/,+$/g, ""); | |
} | |
})(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment