首页 > 解决方案 > 在 background.js 中使用 chrome.runtime.connectNative() 获取错误

问题描述

我正在为 chrome 编写扩展(版本 77.0.3865.90),在尝试在 background.js 中使用 chrome.runtime.connectNative() 后出现错误“ Uncaught TypeError: chrome.runtime.connectNative is not a function ”,这很困惑我,因为我没有在 content.js 中使用它。此功能是否已弃用?

清单.json

{
    "name": "Muter",
    "version": "0.1",
    "description": "Mute html5 video with shortkey.",
    "permissions": ["declarativeContent", "storage", "tabs", "http://*/*", "https://*/"],
    "background": {
        "scripts": ["background.js"],
        "persistent": true
    },
    "page_action": {
        "default_popup": "popup.html"
    },
    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js": ["content.js"],
            "run_at": "document_end"
        }
    ],
    "commands": {
        "pause" : {
            "suggested_key": {
                "mac": "Command+Shift+Z"
            },
            "description": "Pause video"
        }
    },
    "manifest_version": 2
}

背景.js

var videoStack = [];
var isVideoPlaying = true;
var port = chrome.runtime.connectNative('video_controler');

port.onMessage.addListener((response) => {
    console.log('Received: ' + response);
});


主机应用


#include <stdio.h>

int main(void)
{
    printf("pause_or_play\n");
    return 0;
}

标签: javascriptgoogle-chrome-extensionbackground

解决方案


推荐阅读