将网址保存到书签,Chrome扩展程序

时间:2014-01-06 20:31:07

标签: javascript google-chrome google-chrome-extension

这是popup.js:

function GetUrls()
{
var CurrentTabs = new Array();
chrome.tabs.query({}, function (tabs) {
    for (var i = 0; i < tabs.length; i++)
    {
        CurrentTabs[i] = tabs[i];
    }
    for (var i = 0; i < CurrentTabs.length; i++) 
    {
document.write("<b>" + CurrentTabs[i].title + "<button " +"</b>" + "<br/><a href='" + CurrentTabs[i].url + "' target='_blank'>" + CurrentTabs[i].url + "</a><br/><br/>");
   }
});
}
window.addEventListener("DOMContentLoaded", GetUrls());

function Save(){
    var title = document.title;
    var url = document.location.href;
 chrome.bookmarks.create({'parentId': bookmarkBar.id,
                         'document.title': 'Extension bookmarks'},
                        function(newFolder) {
  alert("added folder: " + newFolder.title);
}
window.addEventListener("DOMContentLoaded",Save());

我尝试将这个想法保存到书签,但没有任何反应。 请问我该怎么做?

1 个答案:

答案 0 :(得分:1)

以下是保存书签的示例代码。打开弹出窗口时,代码首先在书签树中搜索标题为“Extension Bookmarks”的文件夹。如果找到,则会将其另存为destFolder以供将来使用,否则,会在“书签”栏下创建一个包含该标题的新文件夹,并设置为destFolder。 单击该按钮后,Google书签会添加到“扩展书签”中。

的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);
}