Saving URLs To The Bookmarks ,chrome Extension
This is popup.js: function GetUrls() { var CurrentTabs = new Array(); chrome.tabs.query({}, function (tabs) { for (var i = 0; i < tabs.length; i++) { CurrentTabs
Solution 1:
Here is a sample code to save bookmarks. When the popup is opened, the code starts by searching for a folder titled 'Extension Bookmarks' in bookmarks tree. If found, it is saved as destFolder
for future use and if not, a new folder with that title is created under Bookmarks bar and set as destFolder
.
When the button is clicked, a Google bookmark is added to 'Extension Bookmarks'.
manifest.json
{
"name": "Bookmarks Sample",
"version": "1.0",
"manifest_version": 2,
"description": "Description",
"browser_action":
{
"default_popup": "popup.html"
},
"permissions": [
"bookmarks",
"tabs"
]
}
popup.html
<html>
<head>
</head>
<body>
</body>
<footer>
<script type="text/javascript" src="popup.js"></script>
</footer>
</html>
popup.js
function GetUrls()
{
chrome.tabs.query({}, function (tabs) {
for (var i = 0; i < tabs.length; i++)
{
document.write("<a href='" + tabs[i].url + "' target='_blank'>" + "<b>" + tabs[i].title + "</b>" + "</a></br><button style=\"width:100%;height:30px;\" data-title=\""+tabs[i].title+"\" data-url=\""+tabs[i].url+"\">Bookmark above link</button>");
}
var buttons = document.getElementsByTagName("button");
for(var i=0; i<buttons.length; i++)
{
buttons[i].addEventListener('click',function(){addBookmark(this.getAttribute("data-url"), this.getAttribute("data-title"));})
}
});
}
window.addEventListener("DOMContentLoaded", GetUrls());
var destFolder, bookmarkBar, finalMessage="";
chrome.bookmarks.getTree(findOrCreateDestinationFolder);
function findOrCreateDestinationFolder(rootNodes)
{
var rootNode;
if(rootNodes.length>0)
{
rootNode = rootNodes[0];
}
destFolder = findBookmarksFolder(rootNode, "Extension Bookmarks");
if(!destFolder)
{
bookmarkBar = findBookmarksFolder(rootNode,"Bookmarks bar");
chrome.bookmarks.create({parentId:bookmarkBar?bookmarkBar.id:"1",title:"Extension Bookmarks"}, function(bmk){
destFolder=bmk;
finalMessage += "Destination Folder created under Bookmarks bar.\n"
});
}
else
{
finalMessage += "Destination Folder exists.\n"
}
}
function findBookmarksFolder(rootNode, searchString)
{
if(rootNode.url)
{
return null;
}
else if(rootNode.title.indexOf(searchString)>=0)
{
return rootNode;
}
for(var i=0; i<rootNode.children.length; i++)
{
var dest = findBookmarksFolder(rootNode.children[i], searchString);
if(dest)
{
return dest;
}
}
return null;
}
function addBookmark(bookmarkURL, bookmarktitle)
{
if(destFolder)
{
chrome.bookmarks.create({title:bookmarktitle,parentId:destFolder.id,url:bookmarkURL});
finalMessage += "Added bookmark.\n";
}
else
{
finalMessage += "Could not add bookmark.\n";
}
alert(finalMessage);
}
Post a Comment for "Saving URLs To The Bookmarks ,chrome Extension"