javascript - chrome.declarativeContent.onPageChanged.removeRules(未定义,函数(){})不工作

标签 javascript google-chrome google-chrome-extension runtime-error declarative

当我尝试使用 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 just ShowPageAction) when a set of conditions, represented as a PageStateMatcher, are met.

因此,不幸的是,您似乎无法使用此 API 调用您的 init()

关于javascript - chrome.declarativeContent.onPageChanged.removeRules(未定义,函数(){})不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24777295/

相关文章:

javascript - 如何处理不立即返回结果的端点

vba - 检查元素是否存在 VBA Selenium

javascript - Chrome 扩展 - onMessage 监听器不工作

javascript - 从外部 JSON 文件获取文件内容

javascript - 从 Google Places API 获取城市地点 ID

javascript - jQuery blur() 不听 $(this)

javascript - Chrome 开发者工具,在脚本标签内使用检查函数

asp.net - WebForm_DoPostBackWithOptions 未使用 chrome 定义

javascript - 将扩展设置为自动对特定 URL 执行操作

css - 带有 webpack style-loader 的 Chrome 扩展找不到样式目标