Skip to content

Instantly share code, notes, and snippets.

@MahbbRah
Created February 20, 2025 09:23
Show Gist options
  • Save MahbbRah/8529f35bc89ab00cc6df0e8d60973874 to your computer and use it in GitHub Desktop.
Save MahbbRah/8529f35bc89ab00cc6df0e8d60973874 to your computer and use it in GitHub Desktop.
These methods returns the audio source URL from Facebook messenger react component.
function getReactFiber(node) {
for (const key in node) {
if (key.startsWith("__reactFiber$")) {
return node[key];
}
}
return null;
}
function findAudioSource(node) {
const fiber = getReactFiber(node);
if (!fiber) return null;
let current = fiber;
while (current) {
if (current.memoizedProps && current.memoizedProps.audioEl) {
return current.memoizedProps.audioEl.current.src; // Extract audio source URL
}
current = current.return; // Traverse up the React tree
}
return null;
}
// Run the function on the selected element in the Elements tab
console.log(findAudioSource($selectedElement));
@shamimurrahman19
Copy link

shamimurrahman19 commented Feb 21, 2025

The following doesn't work in chrome extension.
It can't find react fiber in the page.

function getFiberFromDom(node: HTMLElement | null) {
        // return all object keys of the node
        console.log('Node keys:', Object.keys(node));

        if (Object.keys(node).length === 0) {
            for (const key of Object.keys(node)) {
                if (key.startsWith("__reactFiber$") || key.startsWith("__reactInternalInstance$")) {
                    console.log('Found React Fiber:', node[key])
                }
            }
        } else {
            console.log('Could not find Object.keys');
        }

        return null
    }

    useEffect(() => {
        const handleClick = (event: MouseEvent) => {
            const target = event.target as HTMLElement
            console.log("Clicked DOM node:", target)
            getFiberFromDom(target)
        }

        document.addEventListener("click", handleClick, true)
        return () => {
            document.removeEventListener("click", handleClick, true)
        }
    }, [])

@MahbbRah
Copy link
Author

MahbbRah commented Feb 27, 2025

Just to publish it over here

The reason it doesn't work inside chrome extension is because the scripts executes inside a isolated environment which doesn't have access to React Fiber and all that data.

by research, we found a way to inject the code directly into the browser context instead of an isolated environment

const script = document.createElement("script");
script.src = chrome.runtime.getURL("inject.js");  // ✅ Ensure correct path
script.onload = function () {
    this.remove();  // ✅ Cleanup after execution
};
document.documentElement.appendChild(script);

the solution should be wrapped in an IIFE method with the file named inject.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment