问题
Is there any way to add a frame to the bottom of a web page using a Greasemonkey script?
I want to write a script that adds http://www.google.com
at the bottom of every web page, inside a frame.
I also want the width and height of the frame to be the same as the height of the page that is inside it.
回答1:
This is 3 questions in one.
Adding an
<iframe>
is easy, by itself:$(document.body).append ( '<iframe src="http://{SOME_PAGE_BESIDES_GOOGLE!}.com/">' );
But, Google will not let you load it in an <iframe> -- blocking that with a variety of tricks, and the Terms of Service.
Plenty of other sites -- including Stack Exchange sites -- also block operation from within aniframe
.There is a slight chance that you could load framed-blocked pages in an
<object>
, if, and only if, the target page server has Cross-origin resource sharing (CORS) set to allow it.It's a safe bet that your target page(s) don't have CORS set that way. ;-) But, if they did, then the code would be like:
$(document.body).append ( ' \ <object type="text/html" data="http://www.google.com/"> \ </object> \ ' );
Finally, for those cases when you can actually load an
<iframe>
(or<object>
), you would need to communicate the page size back up to the container/master/target page, from the loaded iframe.To do that, see How can two instances of a userscript communicate between frames?.
For Google on every page, your best bet is to use the Google Custom Search API.
来源:https://stackoverflow.com/questions/13965834/add-a-frame-to-the-bottom-of-a-web-page-using-a-greasemonkey-script-and-set-its