简体   繁体   中英

How do I get the search results from a search engine?

I've been trying to figure out how to let a Greasemonkey user script open up a search engine page in the background and fetch search results. I've tried to look up examples to open up HTML pages, but afaik all examples of requests handle ajax calls instead of html calls.

Any hints would be grateful.

The standard Greasmonkey GM_xmlhttpRequest function ( link to API ) can handle any type of request, not just JSON. Under examples, check out the GET request code snippet.

Watch out though. Search engines like Google will not appreciate the screen scrapping (and will probably block you if you grab too many results too quickly).

I've done something similar.
All you have to do is save the GM_xmlhttpRequest response in a DIV .
With this DIV you can do whatever you want (show, hide, display only some of the content, etc)

Just take a look at my script source code.
I'm positive it will help you.

I know you don't need, Mr. 14k rep, but i'll break it down for you anyway :)

function conectar() calls GM_xmlhttpRequest [GET] and stores only the part of the content that i want to use in #divtempora , which is a dummy div that the user never sees (hidden).
Then function resp_dxlegacy() walks through the dummy div, save the info i want in a variable and calls conectar() again passing this parameter and storing the content in another div, which is, finally, displayed to the user.

Haven't done this in GreaseMonkey (don't know really in fact if you can do it); though if you really want to do it by opening a new tab, and if you're not using any GM-specific stuff in your code (and don't want to run the code automatically, well, this can be an obstacle), you can take a look at Custom Buttons extension.

With it, you can create buttons that have access to Firefox internals and invoke stuff like gBrowser.addTab() .

But working with CB is a bit more tricky than in GM.

These posts may help if you're interested:

Code sample copied from Mozilla:

var newTabBrowser = gBrowser.getBrowserForTab(gBrowser.addTab("http://www.google.com/"));  
newTabBrowser.addEventListener("load", function () {  
  newTabBrowser.contentDocument.body.innerHTML = "<div>hello world</div>";  
}, true); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM