I want my Chrome Extension to interact with a page that includes some simple React components - a table in the middle of a lot of plain javascript/html.
My Extension is NOT built with React.
What script could you inject into a page, via a Chrome Extension, that could access React Elements (get their key values and similar)? A simple example would go a great way to helping me...
Here's what I've done so far:
I can inject a script and append to the body of the page...but am not sure how to "import react" or otherwise take advantage of React code on the page.
Using the React inspector in console I can see a element with rows as children.
How would I get the children of that Element? Their key values, for instance?
If I inject the following script I just get "unexpected identifier" errors:
import React from 'react';
import ReactDOM from 'react-dom';
I've tried variations (in my injected script) on the following...no React is found (I know it is there)...The "Inside FindReact function...1" message is logged but nothing else (so I know my script is injected without error).
console.log('In React script...run sub...');
reactSub();
function reactSub(){
console.log('Inside FindReact function...1');
window.FindReact = function(dom) {
for (var key in dom) {
console.log('Loooping...');
if (key.startsWith("__reactInternalInstance$")) {
var compInternals = dom[key]._currentElement;
var compWrapper = compInternals._owner;
var comp = compWrapper._instance;
console.log('Inside FindReact function...2') + console.log(comp);
return comp;
}
let snapCount = React.Children.toArray(this.props.children).filter((item) => item.props.className === 'criterions').length;
// let rubricTable = FindReact(document.querySelector('.criterions'));
console.log('Inside react script...') + console.log(snapCount);
}
return null;
};
Any help/direction appreciated.