0

I have Script.js file which added to the youtube page dynamically through this code:

   var s = document.createElement('script');
    s.src = chrome.extension.getURL('Script.js');
    (document.head || document.documentElement).appendChild(s);
    s.onload = function () {
        s.parentNode.removeChild(s);
    };

I have two questions :
1- how I can send a message from inserted file to a parent chrome extension.
2- how I can make a cross-origin request from the inserted file..It's display error message in the console

Origin http://www.youtube.com is not allowed by Access-Control-Allow-Origin.
Dot Freelancer
  • 4,083
  • 3
  • 28
  • 50

1 Answers1

0
  1. Use postMessage or custom events (example 1, reference 2, example 3).
  2. After adding the permission to the manifest, you can initiate cross-site requests from Content scripts. Use one of the methods from step 1 to call the Content script from the web page. Make sure that you only request the necessary permissions (eg not *://*/* if you do not need to access all URLs) and validate all input (so that malicious pages cannot abuse your extension).
Community
  • 1
  • 1
Rob W
  • 341,306
  • 83
  • 791
  • 678