如何在特定网域上显示Chrome扩展程序? [英] How to show Chrome Extension on certain domains?

查看:149
本文介绍了如何在特定网域上显示Chrome扩展程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在撰写我的第一个Chrome扩展程序。我已经使用权限,但我在任何地方都可以看到我的按钮。



如何仅在地址上显示按钮我正在写扩展名?

解决方案

虽然来自@Sorter的回答有效,但它是不是解决问题的最佳方法。



首先,它并不总是工作。如果页面使用 history.pushState ,页面动作将消失,直到触发 onUpdated onHighlighted 再次发生事件 Chromium issue 231075 其次,该方法效率低下,因为它会触发每个页面状态更新在所有页面上

使页面操作在某些域上显示的最有效和最可靠的方式是使用 declarativeContent API。这仅适用于Chrome 33之前的版本。在此之前, webNavigation API是最合适的API。这些API优于使用标签 API的方法的优点是您可以安全地使用活动页面,因为您可以声明 URL过滤器。通过这些URL过滤器,只有当您导航到与URL过滤器匹配的页面时,才会触发事件。因此,您的扩展/事件页面将不会被激活,直到真正需要(=没有浪费的RAM或CPU)。

以下是使用 webNavigation background.js ) $ c $ API $:
$ b

onWebNav(details){
if(details。 frameId === 0){
//顶层框架
chrome.pageAction.show(details.tabId);
}
}
var filter = {
url:[{
hostEquals:'example.com'
}]
};
chrome.webNavigation.onCommitted.addListener(onWebNav,filter);
chrome.webNavigation.onHistoryStateUpdated.addListener(onWebNav,filter);

manifest.json


$ b

  {
name:Name,
version:1 ,
manifest_version:2,
background:{
scripts:[background.js],
persistent:false
},
page_action:{
default_title:只在stackoverflow.com上显示

permissions:[
webNavigation


如果您定位到Chrome 33或更高版本,那么您也可以改用declarativeContent API。只需将webNavigation权限替换为declarativeContent,然后使用以下后台脚本(background.js)即可:
$ b

  chrome.runtime.onInstalled.addListener(function(){
chrome.declarativeContent.onPageChanged.removeRules(undefined,function(){
chrome.declarativeContent.onPageChanged.addRules([{
条件:[
new chrome.declarativeContent.PageStateMatcher({
pageUrl:{
hostEquals:'example.com'
}
})
],
actions:[new chrome.declarativeContent.ShowPageAction()]
}]);
});
});

在这两个示例中,我都使用了 UrlFilter ,它与 example.com 域匹配。


I'm writing my first Chrome Extension. I've used permission, but I'm seeing my button everywhere.

How can I only show the button on the addresses I'm writing the extension for?

解决方案

Although the answer from @Sorter works, it is not the best way to solve the problem.

First and foremost, it does not always work. If the page used history.pushState, the page action will disappear and not come back until you trigger the onUpdated or onHighlighted event again Chromium issue 231075.

Secondly, the method is inefficient, because it's triggered for every update of tab state on all pages.

The most efficient and reliable way to get a page action to appear on certain domains is to use the declarativeContent API. This is only available since Chrome 33. Before that, the webNavigation API was the most suitable API. The advantage of these API over the method using the tabs API is that you can safely use event pages, because you can declare URL filters. With these URL filters, the events will only be triggered if you navigate to a page that matches the URL filters. Consequently, your extension/event page will not be activated until really needed (= no wasted RAM or CPU).

Here's a minimal example (background.js) using the webNavigation API:

function onWebNav(details) {
    if (details.frameId === 0) {
        // Top-level frame
        chrome.pageAction.show(details.tabId);
    }
}
var filter = {
    url: [{
        hostEquals: 'example.com'
    }]
};
chrome.webNavigation.onCommitted.addListener(onWebNav, filter);
chrome.webNavigation.onHistoryStateUpdated.addListener(onWebNav, filter);

manifest.json:

{
    "name": "Name ",
    "version": "1",
    "manifest_version": 2,
    "background": {
        "scripts": ["background.js"],
        "persistent": false
    },
    "page_action": {
        "default_title": "Only visible on stackoverflow.com"
    },
    "permissions": [
        "webNavigation"
    ]
}

If you target Chrome 33 and higher, then you can also use the declarativeContent API instead. Simply replace the "webNavigation" permission with "declarativeContent", and use the following background script (background.js):

chrome.runtime.onInstalled.addListener(function() {
    chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
        chrome.declarativeContent.onPageChanged.addRules([{
            conditions: [
                new chrome.declarativeContent.PageStateMatcher({
                    pageUrl: {
                        hostEquals: 'example.com'
                    }
                })
            ],
            actions: [new chrome.declarativeContent.ShowPageAction()]
        }]);
    });
});

In both examples, I used a UrlFilter that matches the example.com domain.

这篇关于如何在特定网域上显示Chrome扩展程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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