Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google chrome chrome扩展将消息从内容脚本发送到_Google Chrome - Fatal编程技术网

Google chrome chrome扩展将消息从内容脚本发送到

Google chrome chrome扩展将消息从内容脚本发送到,google-chrome,Google Chrome,我有一个chrome扩展,它为每个chrome选项卡和add按钮动态创建一个iframe。 我从background.js向content.js发送消息 chrome.tabs.sendMessage(tabs[0].id, {action: "load_ifram~"+id}, function(response) {}); content.js: chrome.extension.onMessage.addListener(function(msg, sender, sendRespons

我有一个chrome扩展,它为每个chrome选项卡和add按钮动态创建一个iframe。 我从background.js向content.js发送消息

chrome.tabs.sendMessage(tabs[0].id, {action: "load_ifram~"+id}, function(response) {});
content.js:

chrome.extension.onMessage.addListener(function(msg, sender, sendResponse) {
split_message = msg.action.split("~");
if (split_message[0] == 'load_ifram') {     
var id = split_message[1];
var height = '35';
var iframe = document.createElement('iframe');      
iframe.src = chrome.extension.getURL('toolbar1.html');
document.documentElement.appendChild(iframe);
}
});
工具栏1.html

<!DOCTYPE html>
<html>
<head>  
<script type="text/javascript" src="test.js"></script>
</head>
<body>
    <input type="text" name="value" id="value" value=""/>       
    <button id="p_button" style="float:right;line-height:15px;margin-top:-1px;font-family:century gothic;">Click ME</button>
</body>

在content.js中,我从background.js获取id。此id应为“我的按钮”的值(单击“我+id”)。我还想在我的按钮单击函数(p_open())中使用此id。我该怎么做?请帮忙!!提前感谢

Chrome提供了Chrome.storage,通过它我们可以将值从后台脚本传递到内容脚本和其他js

要实现这一点,我们需要首先更新清单permisson “权限”:[ “储存”]

document.addEventListener('DOMContentLoaded', function () {
var p_status = document.querySelector('#p_button');
if(p_status ){  
    p_status.addEventListener('click', p_open);
}
});
//to set the storage in background script
chrome.storage.local.set({'id': myid}, function() {});
chrome.storage.local.set({'name': myname},function() {});

//retrive the storage value in other js file
chrome.storage.local.get('id', function (result) {
id= result.id;
});
chrome.storage.local.get('name', function (result) {
name= result.name;
});
chrome.storage.local.get('myname', function (result) {
myname= result.myname;
});