Chrome 清单 v3:runtime.lastError 指定“func”和“files”?

问题描述 投票:0回答:3

我正在到处寻找解决方案,但找不到任何有关此错误的信息。我正在尝试将 Chrome 扩展“更新”为清单 v3,但收到此错误:

Unchecked runtime.lastError: Exactly one of 'func' and 'files' must be specified

有人知道我能做些什么来解决这个问题吗?这是我的清单.json:

{
    "manifest_version": 3,
    "name": "Extension_Name",
    "description": "Extension_Desc",
    "version": "5.0",
    "icons": {
        "16": "/images/image16.png",
        "48": "/images/image48.png",
        "128": "/images/image128.png"
    },
    "action": {
        "default_popup": "popup.html"
    },
    "permissions": [
        "storage",
        "activeTab",
        "scripting",
        "tabs"
    ],
    "host_permissions": [
        "https://example.com/*"
    ]
}

这是我的 popup.html,在错误中引用了它(但没有特定行,只有 popup.html:0):

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="popup.css">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://example.com">
    <title>Extension Test</title>
</head>
<body>
    <h2>Ghost</h2>
    <form name ="form" id="login_form" action="#">
        <input type="text" id="mdp" placeholder="Password"/>
        <input type ="button" name="submit" value="Login" id="login"/>
    </form>
    
    <button id="logout">Logout</button>
    <script src="popup.js"></script>
</body>
</html>

我知道manifest v3不支持unsafe-eval,但我稍后会处理这个错误。如果我删除 unsafe-eval,另一个错误仍然存在,因此两者没有链接。 预先感谢您!

javascript google-chrome google-chrome-extension chrome-extension-manifest-v3
3个回答
0
投票

最后通过在executeScript()函数中添加“files”解决了该错误。之前我:

chrome.scripting.executeScript(
                {
                    target: {tabId: tab.id},
                    function: functionToExecute()
                })

找到的解决方案是:

chrome.scripting.executeScript(
                {
                    target: {tabId: tab.id},
                    files: ['popup.js'],
                    function: functionToExecute()
                })

再次感谢您的帮助!


0
投票

我之前也尝试过添加参数并恢复它,但我忘记在

()
之后删除函数末尾的
func:
。删除这些也可以修复
func
错误。


0
投票

您应该使用

func
function
不是正确的使用键。另外,你不应该传递函数的结果,而应该传递函数:

func: myfunction

而不是:

func: myFunction()

© www.soinside.com 2019 - 2024. All rights reserved.