Safari 扩展获取选项卡位置或标识符

发布于 2024-12-15 09:14:51 字数 324 浏览 1 评论 0原文

我正在开发一个 safari 扩展,其中我需要将特定的数组元素解析为创建的选项卡的每个实例。但是,我需要能够迭代该数组,以便每个选项卡接收不同的元素以在注入的脚本中使用。我使用接收和发送消息结构来执行此操作,但我一生都无法弄清楚如何迭代数组元素。我尝试创建一个充当索引的数组,然后在每次触发消息响应程序函数时递增它,但这由于某种原因不起作用。我还尝试在每次从数组中提取元素时简单地移动数组,但我相信这不起作用,因为在创建选项卡时该函数触发得太快。

我希望能够在每个注入的脚本实例上使用某种枚举器函数来找出选项卡编号,然后将其与全局页面的消息一起解析以返回数组中的正确元素。

非常感谢您的任何和所有帮助。

I am working on a safari extension in which I need to parse a particular array element to each instance of a tab that is created. I, however, need to be able to iterate through the array so that each tab receives a different element to work with in an injected script. I using the receive and send message structure to do this, but I cannot for the life of me figure out how to iterate through the array elements. I tried creating an array that would act as an index, and then incrementing it each time the message responder function was fired, but this didn't work for some reason. I also tried simply shifting the array each time an element was pulled from it, but I believe this didn't work because the function is fired too quickly as tabs are created.

I want to be able to use some sort of enumerator function on each injected script instance to figure out the tab number and then parse that with the message to the global page to return the proper element in the array.

Thanks so much for any and all help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

━╋う一瞬間旳綻放 2024-12-22 09:14:51

我认为您正在尝试遍历所有窗口中的所有选项卡 - 请澄清您遇到问题的数组。为了遍历所有这些,您应该能够这样做,首先通过应用程序中的窗口,然后通过选项卡:

var bWindows = safari.application.browserWindows;
for(i=0;i<bWindows.length;i++){
    var tabs = bWindows[i].tabs;
    for(j=0;j<tabs.length;j++){
         var tab = tabs[j];
         //Do something in each tab.
         tab.page.dispatchMessage('message', data);
    }
}

I think you are trying to iterate through all tabs in all windows -- please clarify which array you are having trouble with. In order to iterate through all of them, you should be able to do it like this, first through the windows in the application, then through the tabs:

var bWindows = safari.application.browserWindows;
for(i=0;i<bWindows.length;i++){
    var tabs = bWindows[i].tabs;
    for(j=0;j<tabs.length;j++){
         var tab = tabs[j];
         //Do something in each tab.
         tab.page.dispatchMessage('message', data);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文