Greasemonkey against an iframe using @include - does this work? [duplicate]

吃可爱长大的小学妹 提交于 2019-12-03 06:30:07
npdoty

Well, it's certainly possible to get Greasemonkey to run against an iframe -- in fact, it's a common question to determine how to stop it from executing on iframes as well as the main page. You should be able to take the converse of that answer to prevent code from executing on the top window:

if (window.top == window.self)  //don't run on the top window
    return;
//rest of the actual executing code goes here

I've tested it and you can use @include to match domain B (the domain of the iframe) and run a piece of arbitrary code that modifies it. I ran the following test userscript on a test page and it successfully hides the Google logo (only when Google is in an iframe).

// @include  http://www.google.com*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==

if (window.top == window.self)  //don't run on top window
    return;

alert('running on a frame');

$('img').each(function() {
  $(this).hide();
});

So far as I can tell, there aren't any cross-domain restrictions involved here. I'm not sure what happens if the iframe isn't present when the page is first loaded (which is when Greasemonkey runs).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!