简体   繁体   English

用于更改 Chrome 设置的 Chrome 扩展程序

[英]Chrome extension to change chrome settings

I am trying to create an extension to set certain settings on chrome such as popups etc.我正在尝试创建一个扩展程序来在 chrome 上设置某些设置,例如弹出窗口等。

I asked for contentSettings permission in my manifesto :我在我的宣言中要求获得contentSettings权限:

 "permissions": [
    "tabs", "http://*/*", "https://*/*","contentSettings"
  ],

and I have the following js code to change the settings我有以下 js 代码来更改设置

var url = 'http://google.com';

var pattern = /^file:/.test(url) ? url : url.replace(/\/[^\/]*?$/, '/*');
var setting = 'popups';
console.log(' setting for '+pattern+': '+setting);

chrome.contentSettings[setting].set({
        'primaryPattern': pattern,
        'setting': 'allow'
      });

and I get the following error in my console:我在控制台中收到以下错误:

Uncaught TypeError: Cannot read property 'popups' of undefined

What am I doing wrong?我究竟做错了什么?

Most chrome.* APIs are not available to content scripts.大多数 chrome.* API 不可用于内容脚本。 They can only be used from background or event pages, popups, or other extension views you define.它们只能在背景或事件页面、弹出窗口或您定义的其他扩展视图中使用。 If you need to initiate your action in response to something that depends on the contents of a page, you can send a message from your content script to a background or event page to carry it out.如果您需要启动您的操作以响应依赖于页面内容的某些内容,您可以从您的内容脚本向后台或事件页面发送消息以执行它。

Your script looks OK but you've spelled the setting wrong!您的脚本看起来不错,但您拼写了错误的设置! It should be “pop-ups” not “popups”.它应该是“弹出窗口”而不是“弹出窗口”。

These strings are predefined constants so you gotta stick to what the Chrome API has declared.这些字符串是预定义的常量,因此您必须遵守 Chrome API 声明的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM