0
我有一個鉻擴展名,它動態地爲每個鉻選項卡添加一個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);
}
});
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:
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)。此外,我想用我的按鈕點擊功能(p_open())這個ID。我怎樣才能做到這一點?請幫忙!!在此先感謝