0

I want to execute on Page js function instead of handling all logic in extension

content script

document.addEventListener("MY_EVENT", function (data) {
   chrome.runtime.sendMessage("myextid");
});

chrome.extension.onMessage.addListener(function (msg, sender) {
 //***********
 //want to move below logic to page instead of here
 //As modifying/uploading extesion is difficult than on page js
 //what I want is call here 
 //extResponse(msg);
 //instead of below code, but it says undefined function
 if (msg.action == 'MY_DATA') {
    $('#txt1').val(msg.id);
    $('#txt2').val(msg.name);
    $('#txt3').val(msg.email);
    $('.myClass').addClass('stop-spining').removeClass('text-center');
 }
 //***********
});

mypage.html

<html>
 <body>

   <script>
      function extResponse(msg){
        //I want to handle all logic here....
      }
   </script>
 </body>
</html>
alamnaryab
  • 1,480
  • 3
  • 19
  • 31
  • Do it as per [Insert code into the page context using a content script](//stackoverflow.com/a/9517879) – wOxxOm Oct 12 '19 at 08:38

1 Answers1

0

What is happening is that content scripts run in an isolated environment from the page. You can access the DOM elements from a content script but you can't call javascript code inside the page from your content script. Check out https://developer.chrome.com/extensions/content_scripts You need to inject your code at page context, a good tutorial is given here Insert code into the page context using a content script

Javier Silva Ortíz
  • 2,864
  • 1
  • 12
  • 21