Is there any way to retrieve a page's javascript variables from a Google Chrome Content Script?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
I actually worked around it using the localStorge API. Note: to use this, our contentscript should be able to read the localStorage. In the manifest.json file, just add the "storage" string:
The hijack function lives in the content script:
Now we can call from the contentscript
or if you use jQuery in your contentscript, like I do:
to extract the content:
This can be called multiple times, so if your page changes elements or internal code, you can add event listeners to update your extension with the new data.
Edit: I've added callbacks so you can be sure your data won't be invalid (had this issue myself)
Using Liran's solution, I'm adding some fix for
Objects
, here's correct solution:I created a little helper method, have fun :)
to retrieve the window's variables "lannister", "always", "pays", "his", "debts", you execute the following:
my code:
please note that i used jQuery.. you can easily use the native js "removeAttribute" and "removeChild" instead.
If you know which variables you want to access, you can make a quick custom content-script to retrieve their values.
In
popup.js
:In
retrieveValue.js
:You can modify the code to return arrays or other objects.
If you really need to, you can insert a
<script>
element into the page's DOM; the code inside your<script>
element will be executed and that code will have access to JavaScript variables at the scope of the window. You can then communicate them back to the content script usingdata-
attributes and firing custom events.Sound awkward? Why yes, it is, and intentionally so for all the reasons in the documentation that serg has cited. But if you really, really need to do it, it can be done. See here and here for more info. And good luck!
No.