This solution is for cases where the iframe no place to run @include or @match .
This works with Greasemonkey 4.
We must wait for each frame to load before we can work with it. I do this with waitForKeyElements.js , which expects elements matching a given CSS selector, just as looking for matches in document.querySelectorAll("selector") , and then applies this function to the response:
// ==UserScript== // @include https://blah.example.com/* // @require https://git.io/waitForKeyElements.js // ==/UserScript== function main(where) { // do stuff here with where instead of document // eg use where.querySelector() in place of document.querySelector() // and add stylesheets with where.head.appendChild(stylesheet) } main(document); // run it on the top level document (as normal) waitForKeyElements("iframe, frame", function(elem) { elem.removeAttribute("wfke_found"); // cheat wfke been_there, use our own for (let f=0; f < frames.length; f++) { if (!frames[f].document.body.getAttribute("been_there")) { main(frames[f].document); frames[f].document.body.setAttribute("been_there", 1); } } });
Note that the selected item is just a placeholder indicating that the iframe loaded. We remove the tracker βbeen thereβ from waitForKeyElements because the frame can be loaded again later (we cannot just use this iframe because its contents are loaded elsewhere).
When we know that the frame is loaded, we iterate over each frame and look for our marker, an HTML attribute in the body frame called been_there (for example, <body been_there="1"> ). If it is missing, we can run our main() function on the frame document. When we finish, we will add the been_there attribute been_there that we no longer start.
Adam Katz Apr 24 '19 at 19:32 2019-04-24 19:32
source share