这是我在 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 }
}
有了这个,我可以创建书签,但存在一些错误,例如创建多个候选列表、书签栏中的多个候选列表文件夹或子文件夹中的多个网址。我无法解码错误中的模式。请帮我解决这个问题。提前致谢。虽然我不认为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"
]
}
最佳答案
开始之前的一些提示
- bck.js 每次 chrome 重启时仅加载一次,这意味着全局变量仅声明和初始化一次。
- chrome.bookmarks.* 是异步的。无论在此 api 之外编写什么函数,都会首先执行,然后再执行 chrome api。
因此将foldercheck全局变量移动到函数中。删除 api 之外的所有 if 条件。
这应该有效
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
}
})
}
请缩进您的代码
关于javascript - Chrome 扩展 - javascript 中的功能无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32325103/