【发布时间】:2017-03-15 15:40:30
【问题描述】:
我为 Chrome 编写了一个扩展程序。我希望当我单击扩展程序中的按钮时,值“abc”将设置为活动页面上的活动输入。 这是我的代码: 1) manifest.json
{
"name": "Test",
"short_name": "Test",
"manifest_version": 2,
"version":"2.0.0.0",
"browser_action": {
"default_popup": "index.html",
"default_title": "Load EmojiSelector"
},
"background":{
"scripts":["background.js"],
"persistent": false
},
"content_scripts":[
{
"matches":["http://*/*", "https://*/*"],
"js":["content.js"]
}
]
,
"permissions": [
"activeTab"
]
}
2) 索引.html
<!DOCTYPE html>
<html>
<head>
<title>Test SendMessage</title>
<script src='content.js'></script>
</head>
<body>
<input id='btsend' type='button' value='Send abc to input'>
</body>
</html>
3) 背景.js
chrome.runtime.onMessage.addListener(function(response, sender, sendResponse){
var act = chrome.tabs.getSelected(null, function(tab){
//How to set the value of response to active input in page????
});
});
4) 内容.js
onload=function(e){
var btsend = document.getElementById('btsend');
btsend.onclick = function(){
chrome.runtime.sendMessage('abc');
}
}
如何使用 DOM 为活动页面中的活动输入设置值。
【问题讨论】:
-
假设您关注 wOxxOm 的回答,您的下一个问题可能会被:Pass a parameter to a content script injected using chrome.tabs.executeScript()
-
这与我的话题有关,但不适合。我想在活动页面中设置 DOM 的值。变量只有设定值。
标签: javascript google-chrome google-chrome-extension