flipperbw / FantasyPlus

Adds FantasyPros projections to fantasy football sites.
Mozilla Public License 2.0
88 stars 38 forks source link

firefox w/greasemonkey #6

Open jedahan opened 10 years ago

jedahan commented 10 years ago

I got this working as a greasemonkey script. The only change I needed to make is $.get needed to be replaced with GM_xmlhttpRequest to get around CORS.

Should be helpful when making a firefox plugin. Not sure if theres an easy way to go from greasemonkey to full fledged firefox plugin (probably with JetPack).

Cheers!

flipperbw commented 10 years ago

That runs asynchronously, right? Is that literally the only change you made?

jedahan commented 10 years ago

Yes it is asynchronous. fetchPositionProjections looks like this now:

    //Get the data from external sites
    function fetchPositionProjections(position, cb) {
        var source_site = '';
        if (window.positions.indexOf(position) > -1) {
            source_site = 'http://www.fantasypros.com/nfl/projections/' + position + '.php?filters=44:45:73:152:469&export=xls';
        }
        else {
            source_site = 'http://www.fantasysharks.com/apps/bert/forecasts/projections.php?csv=1&Position=' + position;
        }

        /* Use GM_xmlhttpRequest for CORS in greasemonkey */
        GM_xmlhttpRequest({
        method: "GET",
        url: source_site,
        onload: function(response) {
            cb(position, response.responseText.trim());
        }
        });

        /*
        $.get(source_site, function(data) {
            cb(position, data.trim());
        });
        */
    }
jedahan commented 10 years ago

In fact, you could probably just add something like

// if in greasemonkey script
if(GM_xmlhttpRequest){
  GM_xmlhttpRequest({
        method: "GET",
        url: source_site,
        onload: function(response) {
            cb(position, response.responseText.trim());
        }
    });
} else {
    $.get....
}

I also prepended the jquery as I am not sure how to use multiple scripts with greasemonkey. I just made one concatenated script.

smartymcfly commented 10 years ago

Is there somewhere that this script is posted to download/add to greasemonkey? I am not very sure how to do it manually.