我收到“没有匹配的签名”我的background.js 中出现错误

发布于 2025-01-11 19:40:20 字数 1658 浏览 0 评论 0原文

我收到错误“Uncaught TypeError:调用 scripting.executeScript 时出错(scripting.ScriptInjection 注入,可选函数回调):没有匹配的签名。”在我的控制台中。 我不知道出了什么问题,请有人帮助我,我真的很感激。

这是background.js 代码:

var Tabclicked = false;
disableBrowserAction();


function disableBrowserAction(tabId) {
    chrome.scripting.executeScript(tabId, { file: 'jquery.js' }, function() {
        chrome.scripting.executeScript(tabId, { file: 'CloseRuler.js' });
    });
}

function enableBrowserAction(tabId) {
    chrome.scripting.executeScript(tabId, { file: 'jquery.js' }, function() {
        chrome.scripting.executeScript(tabId, { files: 'Ruler.js' });
    });
}

function updateState() {
    if (Tabclicked == false) {
        Tabclicked = true;
        enableBrowserAction();
    } else {
        Tabclicked = false;
        disableBrowserAction();
    }
}

//chrome.pageAction.onClicked.addListener(updateState);

chrome.action.onClicked.addListener(function(tabId) {
    updateState(tabId);
    alert('File test alert');

});

alert('File test alert');

这是我的清单:

{
    "name": "Physics Ruler",
    "description": "Position the ruler and measure the objects!",
    "version": "1.0",
    "manifest_version": 3,
    "permissions": [
        "activeTab",
        "scripting"
    ],
    "background": {
        "service_worker": "background.js",
        "type": "module"
    },
    "action": {
        "default_icon": "icon.png",
        "default_title": "Ruler",
        "name": "Physics Ruler"
    },
    "content_scripts": [{
        "js": ["jquery.js"],
        "matches": ["*://*/*"]
    }],
    "host_permissions": [
        "*://*/*"
    ]
}

I get the error "Uncaught TypeError: Error in invocation of scripting.executeScript(scripting.ScriptInjection injection, optional function callback): No matching signature." in my console.
I have no idea what's wrong, someone please help me, I really appreciate it.

Here is the background.js code:

var Tabclicked = false;
disableBrowserAction();


function disableBrowserAction(tabId) {
    chrome.scripting.executeScript(tabId, { file: 'jquery.js' }, function() {
        chrome.scripting.executeScript(tabId, { file: 'CloseRuler.js' });
    });
}

function enableBrowserAction(tabId) {
    chrome.scripting.executeScript(tabId, { file: 'jquery.js' }, function() {
        chrome.scripting.executeScript(tabId, { files: 'Ruler.js' });
    });
}

function updateState() {
    if (Tabclicked == false) {
        Tabclicked = true;
        enableBrowserAction();
    } else {
        Tabclicked = false;
        disableBrowserAction();
    }
}

//chrome.pageAction.onClicked.addListener(updateState);

chrome.action.onClicked.addListener(function(tabId) {
    updateState(tabId);
    alert('File test alert');

});

alert('File test alert');

Here is my manifest:

{
    "name": "Physics Ruler",
    "description": "Position the ruler and measure the objects!",
    "version": "1.0",
    "manifest_version": 3,
    "permissions": [
        "activeTab",
        "scripting"
    ],
    "background": {
        "service_worker": "background.js",
        "type": "module"
    },
    "action": {
        "default_icon": "icon.png",
        "default_title": "Ruler",
        "name": "Physics Ruler"
    },
    "content_scripts": [{
        "js": ["jquery.js"],
        "matches": ["*://*/*"]
    }],
    "host_permissions": [
        "*://*/*"
    ]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱纠缠 2025-01-18 19:40:20

所以,我找到了答案,并且文档有点无帮助,我只是尝试了不同的方法,直到它起作用。

我们必须使用 chrome tabs 来实现这个函数:

chrome.tabs.query({ currentWindow: true, active: true }, function(tabs) {

在该函数中我们可以使用“tabs[0].id”来获取 id。

例如:

function enableBrowserAction(tabId) {
chrome.tabs.query({ currentWindow: true, active: true }, function(tabs) {
    chrome.scripting.executeScript({ target: { tabId: tabs[0].id }, files: ['jquery.js'] }, function() {
        chrome.scripting.executeScript({ target: { tabId: tabs[0].id }, files: ['Ruler.js'] });
    });
});

}

So, I have found the answer, and the documentation was kind of not help full, I have just tried different things until it worked.

We have to make this function with chrome tabs:

chrome.tabs.query({ currentWindow: true, active: true }, function(tabs) {

in that function then we can use "tabs[0].id" to get the id.

for example:

function enableBrowserAction(tabId) {
chrome.tabs.query({ currentWindow: true, active: true }, function(tabs) {
    chrome.scripting.executeScript({ target: { tabId: tabs[0].id }, files: ['jquery.js'] }, function() {
        chrome.scripting.executeScript({ target: { tabId: tabs[0].id }, files: ['Ruler.js'] });
    });
});

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文