我已经为 Google Chrome 编写了一个扩展,可以使用浏览器操作按钮打开和关闭图像加载。
它有效,但到目前为止我只能通过 URL 设置和获取特定页面的值。这些设置显示在设置> Under the Bonnet > Privacy > Content Settings... > Images 部分的Manage exceptions... 按钮下。例如,
chrome.contentSettings['images'].get({
'primaryUrl': 'http://www.example.com/*',
'incognito': false
},
function(details) {
console.log('Show images: ' + details.setting);
});
将输出消息“显示图像:允许”或“显示图像:阻止”。
但我希望能够打开和关闭全局设置。因此,我需要知道在设置 > Under the Bonnet > Privacy > Content Settings... > Images 下的值是“显示所有图像(推荐)”还是“不显示任何图像”,即,允许还是阻止是默认?
对 primaryUrl
使用通配符的各种尝试,例如以下内容,会引发错误:
chrome.contentSettings['images'].get({
'primaryUrl': '*://*/*',
'incognito': false
},
function(details) {
console.log('Show images: ' + details.setting);
});
错误:
'Error during contentSettings.get: The URL "*://*/*" is invalid.'
引用 Content Settings和 Match Patterns让我觉得我需要使用特殊的 <all_urls>
模式,但我也遇到了错误。
最佳答案
对于 contentSettings.get,您可以使用“http://*”或当前选项卡的 url。 '<all_urls>'
适用于 contentSettings.set 中的 primaryPattern。
function toggleImages(tab) {
chrome.contentSettings['images'].get({
primaryUrl: tab.url
}, function (details) {
chrome.contentSettings['images'].set({
primaryPattern: '<all_urls>',
setting: details.setting == 'allow' ? 'block' : 'allow'
})
});
}
chrome.browserAction.onClicked.addListener(toggleImages);
关于javascript - 如何获取/设置扩展程序中图像的全局 Google Chrome 内容设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10590529/