当我尝试使用 chrome.declarativeContent.onPageChanged.removeRules() 删除所有规则时,它显示:
Error in response to events.removeRules: Error: Invalid value for argument 1. Property '.1': Value does not match any valid type choices, Property '.2': Value does not match any valid type choices.
这是代码,我只是从这里复制的:https://developer.chrome.com/extensions/examples/api/pageAction/pageaction_by_url/background.js
// When the extension is installed or upgraded ...
chrome.runtime.onInstalled.addListener(function () {
console.log('installed');
// Replace all rules ...
chrome.declarativeContent.onPageChanged.removeRules(undefined, function () {
console.log('removed');
// With a new rule ...
chrome.declarativeContent.onPageChanged.addRules([
{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: {
urlEquals: 'www.google.com'
}
})
],
actions: [new chrome.declarativeContent.ShowPageAction(), console.log('hi'), init()]
}
]);
});
});
奇怪的是控制台显示(按此顺序):
hi
installed
removed
error above (error in response to...)
如有任何帮助,我们将不胜感激。谢谢。
最佳答案
错误源自新规则中的actions
列表。
actions
显然需要一个函数引用数组
据我所知,Rule
中的actions
列表只能包含API 提供的“actions”,不能执行任意代码。 base documentation对什么是“ Action ”不是很清楚。
但是,从 documentation of chrome.declarativeContent
(强调我的):
As a declarative API, this API lets you register rules on the
onPageChanged
event object which take an action (currently justShowPageAction
) when a set of conditions, represented as aPageStateMatcher
, are met.
因此,不幸的是,您似乎无法使用此 API 调用您的 init()
关于javascript - chrome.declarativeContent.onPageChanged.removeRules(未定义,函数(){})不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24777295/