Chrome扩展名 - 书签2返回不确定的

发布于 2025-02-05 05:45:56 字数 1402 浏览 3 评论 0原文

我正在创建一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从来不烧饼 2025-02-12 05:45:56

您是在递归调用bookmarksarray()的递归调用中 中的相同元素,因为每个循环在0上开始。使用push()将其附加到数组中,而不是写入与bookmarktree中的相同索引。

function bookmarksArray(bookmarkTree) {
  for (var i = 0; i < bookmarkTree.length; i++) {
    bookmarks.push(String(bookmarkTree[i].url));
    console.log(bookmarks[i])
    if (bookmarkTree[i].children) {
      bookmarksArray(bookmarkTree[i].children)
    }
  }
}

You're ovwrwriting the same elements of bookmarks in the recursive calls to bookmarksArray(), since each loop starts at 0. Use push() to append to the array instead of writing to the same indexes as in bookmarkTree.

function bookmarksArray(bookmarkTree) {
  for (var i = 0; i < bookmarkTree.length; i++) {
    bookmarks.push(String(bookmarkTree[i].url));
    console.log(bookmarks[i])
    if (bookmarkTree[i].children) {
      bookmarksArray(bookmarkTree[i].children)
    }
  }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文