Chrome扩展名 - 书签2返回不确定的
我正在创建一个Chrome扩展名,该扩展可以使用CTRL+Shift+数字导航到用户前四本书签;一切进展顺利,对于第一,第三和第四书的功能都很好,但是第二本书标记似乎每次都返回不确定。 我感觉这是由于我正在从Chrome API中获得的书签树浏览的方式,但是我不确定在哪里或如何解决此问题。另一个想法是,必须异步解决此功能,但是我不确定为什么会这样,或者为什么它会与其他书签一起使用。
这是我的JS文件
const bookmarks = [];
chrome.tabs.onCreated.addListener(function(tab) { //refresh bookmarks on new tab opening
var bookmarkTreeNodes = chrome.bookmarks.getTree(
function(bookmarkTreeNodes){
bookmarksArray(bookmarkTreeNodes)
})
})
chrome.commands.onCommand.addListener(function(command){
var bookmarkTreeNodes = chrome.bookmarks.getTree( //also check bookmarks on command execution
function(bookmarkTreeNodes){
bookmarksArray(bookmarkTreeNodes)
})
printArray(); //temp
console.log('command:',command.charAt(8)); //tab number temp
tabSelected = command.charAt(8);
chrome.tabs.update({
url: ("" + bookmarks[tabSelected-1])
})
});
function printArray(){
for(var i = 0; i<bookmarks.length; i++){
console.log(bookmarks[i]);
}
}
function bookmarksArray(bookmarkTree){
for(var i=0; i<bookmarkTree.length; i++){
bookmarks[i] = ("" + bookmarkTree[i].url); //url is insert to each spot as string
console.log("" + bookmarks[i])
if(bookmarkTree[i].children){
bookmarksArray(bookmarkTree[i].children)
}
}
}
I'm creating a chrome extension that allows navigation to the users first four bookmarks using ctrl+shift+number; everything is going well, and functional for the 1st, 3rd and 4th bookmark, but the 2nd bookmark seemingly returns undefined every time.
I have a feeling this is due to the way that I'm navigating the bookmark tree that is get from the chrome api, but I'm unsure where or how I can fix this at all. Another thought is that this function has to be resolved asynchronously, but I'm unsure why that would be, or why it would work with the other bookmarks if that is the case.
This is my js file
const bookmarks = [];
chrome.tabs.onCreated.addListener(function(tab) { //refresh bookmarks on new tab opening
var bookmarkTreeNodes = chrome.bookmarks.getTree(
function(bookmarkTreeNodes){
bookmarksArray(bookmarkTreeNodes)
})
})
chrome.commands.onCommand.addListener(function(command){
var bookmarkTreeNodes = chrome.bookmarks.getTree( //also check bookmarks on command execution
function(bookmarkTreeNodes){
bookmarksArray(bookmarkTreeNodes)
})
printArray(); //temp
console.log('command:',command.charAt(8)); //tab number temp
tabSelected = command.charAt(8);
chrome.tabs.update({
url: ("" + bookmarks[tabSelected-1])
})
});
function printArray(){
for(var i = 0; i<bookmarks.length; i++){
console.log(bookmarks[i]);
}
}
function bookmarksArray(bookmarkTree){
for(var i=0; i<bookmarkTree.length; i++){
bookmarks[i] = ("" + bookmarkTree[i].url); //url is insert to each spot as string
console.log("" + bookmarks[i])
if(bookmarkTree[i].children){
bookmarksArray(bookmarkTree[i].children)
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是在递归调用
bookmarksarray()
的递归调用中 中的相同元素,因为每个循环在0
上开始。使用push()
将其附加到数组中,而不是写入与bookmarktree
中的相同索引。You're ovwrwriting the same elements of
bookmarks
in the recursive calls tobookmarksArray()
, since each loop starts at0
. Usepush()
to append to the array instead of writing to the same indexes as inbookmarkTree
.