我首先右键单击图像。 在单击上下文菜单项时,我正在从我的background_page执行脚本,其中一部分是 -
function getView(info, tab){
chrome.tabs.executeScript(null, {code:'var i1hold = document.createElement("img");'});
chrome.tabs.executeScript(null, {code:'i1hold.setAttribute("id", "i1");'});
chrome.tabs.executeScript(null, {code:'i1hold.setAttribute("src",' + info.srcUrl + ')'});
chrome.tabs.executeScript(null, {code:'document.getElementById("image").appendChild(i1hold);'});
}
此行不起作用: -
chrome.tabs.executeScript(null, {code:'i1hold.setAttribute("src",' + info.srcUrl + ')'});
我也尝试过: -
chrome.tabs.executeScript(null, {code:'i1hold.setAttribute("src",info.srcUrl)'});
如何将src属性设置为与右键单击上下文菜单项的图像相同?
答案 0 :(得分:2)
必须添加引号:
chrome.tabs.executeScript(null,
{code:'i1hold.setAttribute("src", "' + info.srcUrl + '")'});
// ^ ^
此外,将代码连接到一个字符串中更有效,并使用chrome.tabs.executeScript
一次:
function getView(info, tab){
var code = 'var i1hold = new Image;'
+ 'i1hold.setAttribute("id", "i1");'
+ 'i1hold.setAttribute("src", "' + info.srcUrl + '");'
+ 'document.getElementById("image").appendChild(i1hold);';
chrome.tabs.executeScript(null, {code: code});
}