我正在开发一个执行以下功能的chrome扩展程序 -
我无法在扩展程序打开的新chrome选项卡中注入html表。我能纠正什么才能执行第3步? 请参阅下面的代码 -
background.js
var tabId = "";
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
//Show the popup
if (request.message == "Enable_Popup") {
chrome.pageAction.show(sender.tab.id);
}
//Get Landing page Url from the fixed webapge
if (request.message == "Open_LP") {
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendMessage(tab.id, { command: "get_url" },function(response){
var htmlCode = '<!DOCTYPE html><html style="height:100%"><head><link rel="stylesheet" type ="text/css" href="style.css"><!-- Latest compiled and minified CSS --><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script><title>Konan</title></head><body class="myClass" id="fenxiui" style="height:100%"><div class="col-sm-12"><h1 id="head1" style="align:center">Konan</h1></div><br><br><br><br><iframe id="lp" class="col-lg-8 col-md-8 col-sm-8" src='+response+' style="height:100%"></iframe><iframe id="konanVal" class="col-lg-4 col-md-4 col-sm-4" src="'+val+'" style="height:100%"></iframe>';
var url = 'data:text/html,' + encodeURIComponent(htmlCode);
chrome.tabs.create({"url": response}, function(tab1){
chrome.tabs.sendMessage(tab1.id, { command: "insert_ifrm" },function(){})
})})})}})
内容脚本(table.js)
chrome.extension.onMessage.addListener(function (msg, sender, sendResponse) {
if (msg.command && (msg.command == "insert_ifrm")) {
var valHTML = '<!DOCTYPE html><html><head><link rel="stylesheet" type ="text/css" href="style.css"><!-- Latest compiled and minified CSS --><meta charset="utf-8"><title>Konan</title></head><body><table><tr><th></th><td>Extraction 1</td></tr><tr><th>Value 1</th><td></td><td><textarea></textarea></td></tr><tr><th>Value 2</th><td></td><td><textarea></textarea></td></tr><tr><th>Value 3</th><td></td><td><textarea></textarea></td></tr><tr><th>Value 4</th><td></td><td><textarea></textarea></td></tr><tr><th>Value 5</th><td></td><td><textarea></textarea></td></tr></table><br><br><table><tr><th></th><td>Extraction 2</td></tr><tr><th>Value 1</th><td></td><td><textarea></textarea></td></tr><tr><th>Value 2</th><td></td><td><textarea></textarea></td></tr><tr><th>Value 3</th><td></td><td><textarea></textarea></td></tr><tr><th>Value 4</th><td></td><td><textarea></textarea></td></tr><tr><th>Value 5</th><td></td><td><textarea></textarea></td></tr></table><br><br><table><tr><th></th><td>Extraction 3</td></tr><tr><th>Value 1</th><td></td><td><textarea></textarea></td></tr><tr><th>Value 2</th><td></td><td><textarea></textarea></td></tr><tr><th>Value 3</th><td></td><td><textarea></textarea></td></tr><tr><th>Value 4</th><td></td><td><textarea></textarea></td></tr><tr><th>Value 5</th><td></td><td><textarea></textarea></td></tr></table></body></html>';
var ifrm = document.createElement("iframe");
ifrm.setAttribute("srcdoc", "valHTML");
ifrm.style.width = "600px";
ifrm.style.height = "480px";
ifrm.style.align = "right";
document.body.appendChild(ifrm);
}
});
答案 0 :(得分:0)
您的代码似乎正确无误。您是否已将table.js
添加到manifest.json
并确保它在每个页面上运行?它应该看起来像这样:
"content_scripts": [
{
"matches": [ "<all_urls>"],
"js": ["table.js]
}
]