How to inject html into webpage with chrome extension on button click

时间:2015-05-24 21:35:27

标签: javascript google-chrome-extension

I have this in my popup.html

<!DOCTYPE html>
<html>
<body>
     <button id="button">Starting Now</button>
     <script src="jquery-2.1.4.min.js" type='text/javascript'></script>
     <script src="popup.js" type='text/javascript'></script>
  </body>
</html>

This is my popup.js so far...

function dothis(){
  /* ??? */
};

document.addEventListener('DOMContentLoaded', function () {
    document.getElementById('button').addEventListener('click', dothis);
});

I want to inject a bit of html code into the web page on button click; I've already included it in my manifest. How do I do this? Sorry, I'm new to this! I've looked around on stack overflow, but I could really understand any of the answers. Thanks in advance!

1 个答案:

答案 0 :(得分:2)

您需要Content Script才能与该页面进行互动。您可以使用Messaging从弹出窗口向内容脚本发送消息,然后内容脚本可以与页面进行实际交互。