给定由书签打开的moz-extension://URL,如何使用扩展代码切换至标签页? [英] Given a moz-extension:// URL opened by bookmark, how can I switch to tab using extension code?

查看:1194
本文介绍了给定由书签打开的moz-extension://URL,如何使用扩展代码切换至标签页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与我正在处理的其他一些问题有关.

This is related to some other questions I am working on.

说,无论出于何种原因,用户都已将打算从浏览器操作上下文菜单中打开的页面(称为模式moz-extensions://MY-OWN-WEBEXT-ID/*的页面pageURL)标记为书签,并在标签中将其打开,然后打开了许多其他页面标签和其他可能的窗口.用户知道扩展选项卡存在于某处,并且不想继续打开新书签,因此希望使用加载项的浏览器操作上下文菜单找到扩展页面的选项卡.同样,我也不希望我的加载项打开重复的选项卡.

Say, for whatever reason, a user has bookmarked a page (call it pageURL of the pattern moz-extensions://MY-OWN-WEBEXT-ID/*) intended to be opened from a browser action context menu, and opened it in a tab, then opened many other tabs and possibly other windows. User knows the extension tab exists somewhere and doesn't want to keep opening new bookmarks, and so wants to use the add-on's browser action context menu to find the extension page's tab. Likewise, I don't want my add-on to open a duplicate tab.

问题在于,由于附加组件未创建选项卡(书签已创建),因此我没有选项卡ID可以传递给 browser.windows.Window 对象,而不是浏览器窗口对象.

The problem, since the add-on did not create the tab (a bookmark did), I have no tab ID to pass to browser.tabs.update( WebExtTab.id, { active: true } ) or window ID to pass to browser.windows.update( WebExtWindow.id, { focused: true } ). (WebExtWindow referring to a WebExtensions browser.windows.Window object, not a browser window object.

我可以使用 browser.extension.getViews( ) 生成浏览器窗口对象(也称为选项卡)的列表,并检查每个window.location.href确实确实存在URL(以及选项卡)(某处),但是我无法使用该窗口对象将焦点放在选项卡上也没有获取browser.tabs.update()的标签ID.

I can use browser.extension.getViews( ) to generate a list of browser window objects (aka tabs), and checking each window.location.href find that indeed the URL (and thus tab) does exist (somewhere), but I can't use that window object to focus on the tab nor to get a tab ID for browser.tabs.update().

在有多个浏览器窗口的情况下,给定该窗口对象,我什至无法获得合适的浏览器窗口,因为getViews返回的window对象没有用于调用id属性. >.类似于标签页问题.

In the case of multiple browser windows, I can't even get the right browser window to raise up given that window object, because the window objects returned by getViews have no id property with which to call browser.windows.update(). Similar to the tabs problem.

最后,我不能使用 查找选项卡ID,因为url选项必须符合

Finally, I can't use browser.tabs.query( { 'url': pageURL } ) to find the tab ID, because the url option must conform to match patterns, which FORBID using the moz-extension:// scheme.

特别有用的是,如果WebExtensions API允许扩展程序查找属于它自己的所有页面的选项卡和窗口,无论这些页面是由附加组件打开,手动输入,书签还是单击链接.

What would be exceptionally useful was if the WebExtensions API allowed an extension to find the tabs and windows of all pages that belong to itself, regardless if those pages were opened by the add-on, manually entered, a bookmark or clicking a link.

例如,给定一个符合moz-extension://MY-OWN-WEBEXT-ID/*的pageURL,可以对与上述模式匹配的url进行browser.tabs.query和/或browser.windows.query,并分别返回WebExt选项卡/窗口对象.如果WebExt API(即书签)未打开这样的选项卡/窗口,则生成一个新对象(即伪创建),以现有数据(即location.href,状态标志等)填充并生成新的对象.所需的数据(即ID号),以便返回的对象可在API的上下文中使用.

For example, given a pageURL conforming to moz-extension://MY-OWN-WEBEXT-ID/*, one could do a browser.tabs.query and/or a browser.windows.query on a url matching the above pattern, and return a WebExt tab/window object, respectively. If such a tab/window was not opened by the WebExt API (i.e. bookmark), then generate a new object (i.e. a pseudo-create), to populate with existing data (i.e. location.href, status flags, etc) and generate new data as needed (i.e. the ID numbers), such that the returned object is usable within the context of the API.

这将填补API覆盖范围的空白,其中某些方法(即getViews)会返回没有钩子且与WebExt API无关的死胡同的浏览器对象,因此大多无用.

This would fill a gap in API coverage where certain methods (i.e. getViews) return dead-end browser objects which have no hooks and no connection with the WebExt API and are thus mostly useless.

推荐答案

简单的答案:++ RTFM. browser.windows.getAll() 将允许您填充带有选项卡信息的Windows对象.您需要manifest.json中的permissions: [ "tabs" ]来获取tab.url属性.但是除此之外,所有窗口和选项卡对象都将具有一个ID,以便您可以轻松地聚​​焦窗口并切换活动选项卡!

The simple answer: ++RTFM. browser.windows.getAll() will allow you to populate the windows objects with tab info. You need the permissions: [ "tabs" ] in manifest.json to get the tab.url property. But other than that, all the windows and tab objects will have an ID so that you can trivially focus window and switch active tab!

注意:这需要Firefox 52.0+才能使用异步/等待功能.否则,您只需要使用函数生成器和Promise.另外,出于演示目的,我省略了任何错误检查,但是稍后再将它们放回去可能是一个好主意.

Note: This requires Firefox 52.0+ to make use of the async/await feature. Otherwise, you just have to use function generators and promises. Also, I've omitted any error checking, for demonstration purposes, but it might be a good idea to put them back in later.

async function tabCreate ( opts ) {
    var pageURL = browser.runtime.getURL( opts.page + '.html' );
    var extWins = await browser.windows.getAll( { populate: true, windowTypes: [ 'normal' ] } );

    // Look for tab by comparing url, if url matches (i.e. tab exists), then focus window and make tab active.
    for ( var extWin of extWins ) {
        for ( var extTab of extWin.tabs ) {
            if ( pageURL === extTab.url ) {
                console.log( `My Extension->tabCreate(): Window ${extWin.id}, Tab ${extTab.id}:\n\t${extTab.url}` );
                browser.windows.update( extWin.id, { focused: true } );
                browser.tabs.update( extTab.id, { active: true } );
                return;
            }
        }
    }

    // Otherwise, create tab.
    browser.tabs.create( { url: pageURL } );
}

意见:我希望我不必为此功能放弃选项卡权限.如果我们总是拥有自己的moz-extension://MY-OWN-WEBEXT-ID/*网址和其他选项卡的null网址,而没有授予访问所有选项卡的权限,那就很好了.

Opinion: I wish I didn't have to give away the tabs permission just for this feature. It would be nice if we always got our own moz-extension://MY-OWN-WEBEXT-ID/* urls, and null URLs for other tabs, without permissions given to access all tabs, but oh well.

示例用法:

function myWebExt_Options ( ) {
    tabCreate( {
        'page': 'options',
        'panel': 1
    } );
}

browser.contextMenus.create( {
    title: 'Options',
    contexts: [ 'browser_action' ],
    onclick: myWebExt_Options
} );

注意:我已经实现了此功能,以期望具有page属性的opts对象中的选项,我将其用作生成完整页面URL的简写.这是由于另一个问题,该问题需要将消息传递到页面,该页面存储在opts.panel中.但是,这些都不是必须的.可以将其更改为扁平字符串,也可以将其他位置生成的完整"getURL"用作参数.进行更改以适合您的需求和风格.

Note: I've implemented this to expect options in an opts object that has a page property, which I use as a shorthand to generate the full page URL. This is because of another question which requires passing a message to the page, which I store in opts.panel. But none of that is necessary. It could be changed to a flat string, or use the full 'getURL' generated elsewhere as a parameter. Change to suit your need and style.

这篇关于给定由书签打开的moz-extension://URL,如何使用扩展代码切换至标签页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆