2

Say I have a webpage like this

<html>
    <head>
        <script>
        function getData() {
            return 1
        }
        </script>
    <!head>
    <body>
        <p>Hello!</p>
    <!body>
<!html>

and for some reason I want to call getData() from a chrome extension and get the result from the function, how would I do that?

Radu Diță
  • 13,476
  • 2
  • 30
  • 34
MrShoe
  • 31
  • 6

1 Answers1

0

Yes you can.

You'll need to inject a script inside the DOM. Extensions share the same DOM as the host, but different JS contexts (so you have a sandbox).

To do this you'll need to add a content script and then inject a JS script (either via DOM api that load an existing script or inlining your code).

From the injected script you'll be able to call window.getData for instance and then you can use window.postMessage to communicate with your extension back.

Radu Diță
  • 13,476
  • 2
  • 30
  • 34