iitc-project / ingress-intel-total-conversion

ingress.com/intel total conversion user script with some new features. Should allow easier extension of the intel map.
http://iitc.jonatkins.com/
ISC License
989 stars 552 forks source link

[Question] programmatically selecting first search result #1146

Closed MTRNord closed 8 years ago

MTRNord commented 8 years ago

Hi I am searching for a way to do easy selecting the first search result or to at least detect if any results are there. I do that in phantomjs not as an plugin. My current code which is very unstable (because of the click() event) looks like:

    if (document.querySelector('#search')){
      window.search.doSearch(search, true)
      var checkExist = setInterval(function() {
        if (document.querySelector('.searchquery').childNodes[1].childNodes[0] != null) {
          console.log('click')
          document.querySelector('.searchquery').childNodes[1].childNodes[0].click();
          clearInterval(checkExist);
        }else{
          console.log('notClicked')
        }
      }, 100);
    }

is there a way to get the results without needing to do the document.querySelector('.searchquery').childNodes[1].childNodes[0].click(); part? because it fails in 99% of my tries. Or is this impossible without doing the search myself and manually going to that position?

MTRNord commented 8 years ago

Also: this aproach does most of the time just search but not click

ZasoGD commented 8 years ago

if you have uploaded IITC should be comfortable use the special hook "search".

window.addHook('search', yourFunctionName);

MTRNord commented 8 years ago

does that give me the already found results? because in the iitc code I saw that always as an search handler for services to search at.

MTRNord commented 8 years ago

Nevermind I do see that I will try that :) (I close when it confirmed does work)

ZasoGD commented 8 years ago

The hook return an object. there is an 'results' array. This is an example (work for the portals):

window.myFunc = function(r){
    console.log(r);
    if(r.results[0] && r.results[0].position && r.results[0].title !== 'No results on OpenStreetMap'){
        window.map.setView(r.results[0].position);
    }
}
window.addHook('search', window.myFunc);
MTRNord commented 8 years ago

@ZasoGD r.results[0] gives me undefinded even if the result array contains elements

fkloft commented 8 years ago

You could try to fire the hook yourself and let the plugins report their results directly to your code. You just have to provide the same API for providing results as IITC. Note that results may come in asynchronously, especially for OSM and missions.

MTRNord commented 8 years ago

ok fixed it by doing setInterval() stuff while waiting that the array is bigger than 0