2013-11-28 3 views
0

으로 메시지 보내기 각 크롬 탭에 iframe을 동적으로 추가하고 버튼을 추가하는 크롬 확장 프로그램이 있습니다.크롬 확장 프로그램 콘텐츠 메시지에서

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); 
} 
}); 

toolbar1.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> 

test.js :

background.js에서 은 내가

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

content.js을 content.js에 메시지를 보내

document.addEventListener('DOMContentLoaded', function() { 
var p_status = document.querySelector('#p_button'); 
if(p_status){ 
    p_status.addEventListener('click', p_open); 
} 
}); 

content.js에서 나는 background.js에서 ID를 얻고 있습니다. 이 ID는 내 버튼의 값이어야합니다 (ME + id 클릭). 또한이 id 내 단추 클릭 함수 (p_open()) 사용하고 싶습니다. 내가 어떻게 할 수 있니? 도와주세요!! 미리 감사드립니다.

답변

1

크롬은 백그라운드 스크립트에서 콘텐츠 스크립트 및 기타 js 값을 전달할 수있는 chrome.storage를 제공합니다.

은 우리가 처음 매니페스트 permisson를 업데이트 "권한"필요이를 구현하기 : [ "저장"]

//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; 
}); 
관련 문제