具有declarativeContent.RequestContentScript的Chrome扩展 [英] Chrome extension with declarativeContent.RequestContentScript

查看:88
本文介绍了具有declarativeContent.RequestContentScript的Chrome扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个Chrome扩展程序,该扩展程序将监视GMail并在用户开始编写消息时执行某些操作。经过对示例和文档的研究,我发现我应该使用declarativeContent来完成,它对页面的更改有反应。



这是我现在所做的。 / p>

manifest.json:

  {
manifest_version :2,
名称: Gmail助手,
版本: 0.1,
权限:[ declarativeContent],
背景: {
scripts:[ background.js],
persistent:假
}
}

background.js:

  chrome.runtime.onInstalled.addListener (函数(详细信息){
chrome.declarativeContent.onPageChanged.removeRules(未定义,函数(){
chrome.declarativeContent.onPageChanged.addRules([{
条件:[
新chrome.declarativeContent.PageStateMatcher({
pageUrl:{hostEquals:'mail.google.com',schemes:['https']},
css:[ div]
// css:[ div [ aria-label ='邮件正文']]
})
],
操作:[new chrome.declarativeContent.RequestContentScript({js:[ content.js]})]
}]);
});
});

content.js:

 警报(测试); 

我的计划是声明一个内容脚本,该脚本将触发GMail中的页面更改。我添加了一个声明性规则,其中定义了pageURL,css和action。根据我的理解,应该在pageUrl和CSS内容匹配时执行content.js。但是,不会执行content.js。



我在做什么错了?



谢谢。

解决方案

在网站上运行内容脚本需要该网站的权限,declarativeContent API文档中没有明确说明,但原因是缺少其他操作中出现的没有主机权限就可以使用此操作注释。 declarativeContent API的目的是在没有任何权限确认警告的情况下从WebStore安装扩展,因此,除非您在清单中明确添加了该API,否则该API自然不会授予您访问mail.google.com的权限。

 权限:[ declarativeContent, https://mail.google.com/] 


I am trying to make a Chrome extension, which will monitor GMail and do something when user starts to write a message. After some study of examples and documentation I have figured out that I should do it with declarativeContent, which reacts on page change.

This is what I have done by now.

manifest.json:

{
  "manifest_version": 2,
  "name": "Gmail helper",
  "version": "0.1",
  "permissions": [ "declarativeContent" ],
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  }
}

background.js:

chrome.runtime.onInstalled.addListener (function (details) {
  chrome.declarativeContent.onPageChanged.removeRules (undefined, function () {
    chrome.declarativeContent.onPageChanged.addRules ([{
      conditions: [
        new chrome.declarativeContent.PageStateMatcher({
          pageUrl: { hostEquals: 'mail.google.com', schemes: ['https'] },
          css: ["div"]
//          css: ["div[aria-label='Message Body']"]
        })
      ],
      actions: [ new chrome.declarativeContent.RequestContentScript({js: ["content.js"]}) ]
    }]);
  });
});

content.js:

alert ("Test");

My plan was to declare a content script that would trigger on page changes in GMail. I have added a declarative rule, which has pageURL, css and actions defined. According to my understanding content.js should be executed when pageUrl and css content match. However, content.js is not executed.

What am I doing wrong?

Thanks.

解决方案

Running a content script on a site requires permissions for the site, which isn't stated explicitly in the documentation of declarativeContent API, but is deduced by the lack of "This action can be used without host permissions" note, which is present in other actions. The purpose of declarativeContent API is to install extensions from the WebStore without any permission confirmation warning so naturally this API can't grant you access to mail.google.com unless you add it explicitly in your manifest:

"permissions": ["declarativeContent", "https://mail.google.com/"]

这篇关于具有declarativeContent.RequestContentScript的Chrome扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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