我是编码的新手,对于这个传递业务的消息我仍然有点不稳定。我想要一个onclick函数将我的popup.html中的textarea插入到我的content.js中的特定位置。在这样做时,它将在所有网页上取出指定的单词,并将其替换为从第二个框(myFunction1()
)提交的单词。如果可能,如果有onClick
方法提交文本区域中的内容并将其写入popup.html,或将其发布在选项页面上,那么这也非常有用。这是我到目前为止所做的:
的manifest.json
{
"manifest_version": 2,
"name": "My extension",
"description": "help!",
"version": "0.1",
"options_page": "options.html",
"icons": { "19" : "icon.png",
"48" : "logo45.png",
"128" : "logo120.png"},
"browser_action": {
"default_icon": "icon.png",
"default_title": "My Extension",
"default_popup": "popup.html"
},
"permissions": [
"activeTab"
],
"permissions" : [
"tabs", "http://*/*"
],
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*"
],
"js": [
"content.js"
],
"run_at": "document_end"
}
]
}
content.js
var port = chrome.runtime.connect({name:"mycontentscript"});
port.onMessage.addListener(function(message,sender){
if(message.greeting === "hello"){
function myFunction() {
var x = document.getElementById("myText").value;
document.getElementById("demo").innerHTML = x;
}
function myFunction1() {
var x = document.getElementById("myText1").value;
document.getElementById("demo1").innerHTML = x;
}
var elements = document.getElementsByTagName('*');
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
for (var j = 0; j < element.childNodes.length; j++) {
var node = element.childNodes[j];
if (node.nodeType === 3) {
var text = node.nodeValue;
var replacedText = text.replace(id="**demo**"/gi, id="**demo1**");
if (replacedText !== text) {
element.replaceChild(document.createTextNode(replacedText), node);
}
}
}
}
popup.html
<input type="text" id="myText" value=" word">
<input type="text" id="myText1" value=" word2">
<button onclick="myFunction();myFunction1()">Thanks!</button>
<p id="demo">
<p id="demo1">
<script src="popup.js"></script>
popup.js
document.addEventListener('DOMContentLoaded',
function myFunction() {
var x = document.getElementById("myText").value;
document.getElementById("demo").innerHTML = x;
};
function myFunction1() {
var x = document.getElementById("myText1").value;
document.getElementById("demo1").innerHTML = x;
}
chrome.runtime.sendMessage({
'method': 'getInfo'
}, function(response) {
console.log(response);
});
});
因此,正如您所看到的,我正在尝试从myText1
和myText
提交文本,以将其放在content.js中以粗体显示的位置。只要函数仍然启用,我希望提交的文本保持运行。此外,由于某种原因,我将文本插回到弹出窗口()的位置没有显示出来。有没有办法做这些事情?