这是我关于stackoverflow的第一个问题。我正在尝试为chrome扩展编写代码,以帮助列入候选名单。我们的想法是在书签栏中有一个名为Shortlists的文件夹,以及名称为“www.stackoverflow.com”或“www.amazon.com”或“www.bbcnews.com”等的几个子文件夹。当我发现一些有趣的但是想要稍后阅读,我可以点击浏览器栏中的“短名单扩展图标”,这将自动创建书签(如果需要,还会显示父文件夹)。我之前没有javascript编程经验。我编写了以下代码--bck.js,由manifest.json
调用 var foldercheck = false;
chrome.browserAction.onClicked.addListener(function(tab){
var taburl = tab.url
var urlsplit = taburl.split("://")
var actual_url
if (urlsplit.length == 2)
{
actual_url = urlsplit[1]
}
else
{
actual_url = urlsplit[0]
}
var website = actual_url.split("/")
website = website[0]
console.log ('taburl: '+ taburl + ' actual_url: '+ actual_url+' website: ' + website )
createShortList(website,taburl)
});
function createShortList(website,taburl) {
console.log(website + ' ' + taburl)
chrome.bookmarks.getTree(function(bookmarks){
chrome.bookmarks.search("Shortlists", function(slist){
console.log ("slist")
if (slist[0]){
console.log ("slistw2")
chrome.bookmarks.getChildren(slist[0].id,function(slistchildren){
slistchildren.forEach(function (slistchild){
if (slistchild.title == website)
{
chrome.bookmarks.create({'parentId' : slistchild.id, 'title' : taburl , 'url' : taburl})
console.log('added in Shortlists1, '+slistchild.id +' ')
foldercheck = true
}
})})}
if (foldercheck == false)
{
chrome.bookmarks.create({'parentId' : slist[0].id, 'title' : website}, function(folder) {
chrome.bookmarks.create({'parentId' : folder.id, 'title' : taburl, 'url' : taburl})
console.log('added in Shortlists2, '+folder.id +' ')
foldercheck = true
})
foldercheck = true
}
})
})
if (foldercheck == false) {
chrome.bookmarks.create({'parentId': "1", 'title': 'Shortlists'}, function(shortlist) {
chrome.bookmarks.create({'parentId' : shortlist.id, 'title' : website}, function(folder2)
{chrome.bookmarks.create({'parentId' : folder2.id, 'title' : taburl, 'url' : taburl});
console.log('added in Shortlists3, '+folder2.id +' ')
;})})
foldercheck = true }
}
有了这个,我可以创建书签,但有一些错误,如创建多个短名单,书签栏中的多个短名单文件夹或子文件夹中的多个网址。我无法解码bug中的模式。请帮我解决这个问题。提前致谢。虽然我不认为manifest.json中存在问题,但这里是供参考的文件。
{
"name": "Shortlist",
"manifest_version" : 2,
"version" : "1.0",
"description" : "To ShortList",
"background" : {"scripts" : ["bck.js"], "persistent" : false},
"browser_action": {"default_icon" : "icon.png"},
"permissions": [
"activeTab", "bookmarks"
]
}
答案 0 :(得分:2)
在我开始之前有几点指示
所以将foldercheck全局变量移动到函数中。除去apis之外的任何条件。
这应该有效
function createShortList(website,taburl) {
foldercheck = false;
chrome.bookmarks.search("Shortlists", function(slist){
// Shortlist folder exists
if (slist[0]){
// Folder with website name exists
chrome.bookmarks.getChildren(slist[0].id,function(slistchildren){
slistchildren.forEach(function (slistchild){
if (slistchild.title == website) {
chrome.bookmarks.create({'parentId' : slistchild.id, 'title' : taburl , 'url' : taburl})
foldercheck = true
}
})
// Folder with website name doesnt exist
if (foldercheck == false){
chrome.bookmarks.create({'parentId' : slist[0].id, 'title' : website}, function(folder) {
chrome.bookmarks.create({'parentId' : folder.id, 'title' : taburl, 'url' : taburl})
foldercheck = true
})
}
})
}
// Shortlist folder does not exist
else{
chrome.bookmarks.create({'parentId': "1", 'title': 'Shortlists'}, function(shortlist) {
chrome.bookmarks.create({'parentId' : shortlist.id, 'title' : website}, function(folder2){
chrome.bookmarks.create({'parentId' : folder2.id, 'title' : taburl, 'url' : taburl})
})
})
foldercheck = true
}
})
}
请缩进代码