18910140161

JavaScript-使用index.html(主流网页,而不是popup.html)如何只在onClick方法之后将数据从content.js发送到background.js?-堆栈溢出

顺晟科技

2022-10-19 14:01:36

141

整个要点是,当激发onclick时,将一些数据从HTML页面(不是弹出窗口)发送到content.js页面,然后将其发送到background.js文件,以对数据执行某些功能,但我在获取这些数据时遇到了一些错误。

index.html

<input type="text" id='fullname' />
<button onclick="sendData()">Submit</button>

content.js

chrome.runtime.sendmessage()方法在document.onload()中运行良好,但在普通函数中调用它时出现问题

<input type="text" id='fullname' />
<button onclick="sendData()">Submit</button>

background.js

<input type="text" id='fullname' />
<button onclick="sendData()">Submit</button>

顺晟科技:

Chrome扩展不允许内联JavaScript(documentation)。

相反,为按钮指定一个id,并使用addeventListener()将函数绑定为事件。

content.js

<input type="text" id='fullname' />
<button onclick="sendData()">Submit</button>
  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航