Google chrome extension Chrome扩展:传递参数时出现问题

Google chrome extension Chrome扩展:传递参数时出现问题,google-chrome-extension,Google Chrome Extension,这是有效的: function click(e) { chrome.tabs.executeScript(null, { code: 'var money = 1;' }, function() { chrome.tabs.executeScript(null, {file: 'peace.js'}); }); } 这不是(为了方便编辑代码): 我怎样才能正确地通过它? 谢谢 找到了一个解决方法: if (e.target.id == '

这是有效的:

function click(e) {
    chrome.tabs.executeScript(null, {
        code: 'var money = 1;'
    }, function() {
        chrome.tabs.executeScript(null, {file: 'peace.js'});
    });
}
这不是(为了方便编辑代码):

我怎样才能正确地通过它? 谢谢

找到了一个解决方法:

if (e.target.id == 'test1') {
    chrome.tabs.executeScript(null, {
        code: 'var money = 1'
    }, function() {
        chrome.tabs.executeScript(null, {file: 'peace.js'});
    });
} else  if (e.target.id == 'test2') {
    chrome.tabs.executeScript(null, {
        code: 'var money = 2'
    }, function() {
        chrome.tabs.executeScript(null, {file: 'test.js'});
    });
}

我认为您的问题是字符串值的格式不正确。比如说,

function click(e) {
    var test = 'test';
    chrome.tabs.executeScript(null, {
        code: 'var money = ' + test + ';'
    }, function() {
        chrome.tabs.executeScript(null, {file: 'peace.js'});
    });
}
不起作用,因为当
var money=test时,脚本不知道什么是
test

如果你想把绳子传过去,它应该是

function click(e) {

    var test = 'test';
    chrome.tabs.executeScript(null, {
        code: 'var money = "' + test + '";'
    }, function() {
        chrome.tabs.executeScript(null, {file: 'peace.js'});
    });
}

这样,执行的代码将是
var money=“test”

你看到的错误是什么?上面写着未定义,@Jianweichuah你有没有试着研究一下
e
是什么?将
console.log(e)
添加到函数中,查看它是什么。是的,当我输入console.log时,它会正确显示。只是没有传递到
peace.js
我认为这只是将js代码注入页面,而不是传递到
peace.js
。如何
var money=1工作?可移植的方法是进行JSON编码。请参阅副本。
function click(e) {

    var test = 'test';
    chrome.tabs.executeScript(null, {
        code: 'var money = "' + test + '";'
    }, function() {
        chrome.tabs.executeScript(null, {file: 'peace.js'});
    });
}