1

I'm working on a Greasemonkey script, and would like to tally a specific value from an attribute. Problem is, the attribute values I want are paginated. I've tried changing the URL to see if it can list everything in one page, but no luck. It always limits to a view of only 40 per page.

I was wondering if the best way to achieve this is by incrementing a value in the URL, by using an if and then statement.

For instance, if a certain element is present (.standard-row), the start=0 in the URL will increment by 40 to start=40, then automatically reload the incremented URL and scan again, if the specific element (.standard-row) is present again, increment by another 40, to start=80. All the while storing the attribute values its fetching from each page.

When the specific element (.standard-row) is no longer visible, it will move onto tallying the attribute values it collected.

Below is the URL I'm trying to increment. The portion of the URL I would like to increment is "start=".

https://play.google.com/store/account?start=0&num=40

The code I listed below is what I'm using to tally the attribute value. It works great for one page, however, I would like to fetch all the attribute values from the paginated pages. If possible.

var total = 0;
$("#tab-body-account .rap-link").each(function() {
  var price = +($(this).attr("data-docprice").replace(/[^\d\.]/g, ""));
  total += price;  
});
$('.tabbed-panel-tab').before('<div id="SumTotal">*Combined Value: $'+ total.toFixed(2) +'</div>');

Thanks in advance for any advice.

user1774640
  • 137
  • 1
  • 2
  • 6

1 Answers1

2

Use GM_setValue() and GM_getValue() to store the total between pages. Check the state of the start parameter and whether an .standard-row element exists.

Something like this:

var startParam      = location.search.match (/\bstart=(\d+)/i);
if (startParam) {
    var totalPrice  = 0;
    var startNum    = parseInt (startParam[1], 10);
    if (startNum    === 0) {
        GM_setValue ("TotalPrice", "0");
    }
    else {
        totalPrice  = parseFloat (GM_getValue ("TotalPrice", 0) );
    }

    $("#tab-body-account .rap-link").each( function () {
        var price   = $(this).attr ("data-docprice").replace (/[^\d\.]/g, "");
        if (price) {
            price   = parseFloat (price);
            if (typeof price === "number") {
                totalPrice += price;
            }
        }
    } );
    //console.log ("totalPrice: ", totalPrice.toFixed(2) );

    $('.tabbed-panel-tab').before (
        '<div id="SumTotal">*Combined Value: $'+ totalPrice.toFixed(2) +'</div>'
    );

    GM_setValue ("TotalPrice", "" + totalPrice);

    if ( $(".standard-row").length ) {
        startNum       += 40;
        var nextPage    = location.href.replace (
            /\bstart=\d+/i, "start=" + startNum
        );
        location.assign (nextPage);
    }
}


Note:
Be sure to use the @grant directive for the GM_ functions. Like so:

// ==UserScript==
// @name     _YOUR SCRIPT NAME
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @grant    GM_setValue   
// @grant    GM_getValue   
// ==/UserScript==
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Thanks Brock for the response. I'm having a little trouble getting it to work. I had to change the .standard-row selector to .snippet.snippet-tiny. Though, the code does'nt seem to run with the following line GM_setValue ("TotalPrice", totalPrice);. When I remove GM_Setvalue, the code runs by tallying for each page, and incrementing the url. Though, it wont se the final tally. Do I need to modify GM_setvalue. Thanks again for you expert help. – user1774640 Nov 05 '12 at 15:26
  • No, the `GM_setValue` is correct. Something not discernible from the question is the problem. Perhaps the `@grant` values are incorrect (see updated answer) or you are injecting the code? Link to your complete script at http://pastebin.com/. – Brock Adams Nov 05 '12 at 20:36
  • Right now I'm just using your modified code only, just to see if the URL will increment and total the attribute values. Here's the pastebin of what I'm trying http://pastebin.com/m2VH0GUZ I didn't have the @grant directives set, though after I set them, it still doesn't increment the the URL. If I remove the GM_setValue and enter URL https://play.google.com/store/account?start=0&num=40, it will increment the URL, though the URL wont increment with GM_setValue added. – user1774640 Nov 05 '12 at 21:42
  • Unfortunately, I can't test against that page without buying something from Google Play, which I will ***never*** do. Might ask you to save a snapshot, sanitized of personal info only. – Brock Adams Nov 05 '12 at 22:10
  • The behavior doesn't make sense unless there is something else in that script or `totalPrice` is not a simple number. What does Firefox's error console (Ctrl-Shift-J) say?   Place `alert(totalPrice)` just before the `GM_setValue`, what does it alert?   Open `about:config` and filter for `greasemonkey.scriptvals`. What does that show? – Brock Adams Nov 05 '12 at 22:14
  • Thanks again for your advice. I recently played around with the code and removed ".toFixed(2)". After removing that, the script seemed to work as intended, but not 100%. It seems to total the values for the first page only, though it only collects the values for the rest of the pages, and displays the values and the total for the first page. I think the problem might be that its not colecting simple numbers? I posted a screen shot of the final result after it go's through all the pages. http://imgur.com/E2Jqj – user1774640 Nov 08 '12 at 14:15
  • Use the code from the just-updated answer. Answer the 3 questions I asked in the preceding comment. – Brock Adams Nov 08 '12 at 23:07
  • Thanks for your patience. Here's screenshots of the question you requested http://i.imgur.com/MBJQS.jpg Here's the pastebin of the new code I used http://pastebin.com/yY5JG2uA – user1774640 Nov 09 '12 at 01:31
  • Hmm. It looks like some additional checks are needed and the selectors need tuning. This is proving to be quite tedious without the actual pages. So, **please do the following:** (1) Temporarily turn off Greasemonkey or the script for these pages, (2) Use Firefox to save the pages to disk. (3) Use a text editor to edit the two main HTML files -- mask your name and account number, but *nothing else*. (4) Upload these two page's HTML files to pastebin and link to them here. – Brock Adams Nov 09 '12 at 02:14
  • Oops, I just realized that the saved value of `TotalPrice` was probably corrupt from earlier versions of the script. Try the new answer-code. If that doesn't work, provide the 2 HTML pages as requested in the previous comment. – Brock Adams Nov 09 '12 at 02:24
  • Tried new code, didn't seem to work. Here's my pages. Page 1- http://pastebin.com/8jgDet1b Page 2- http://pastebin.com/phJd4ZT3 Page 3- http://pastebin.com/UciUra21 Thanks for you help. – user1774640 Nov 09 '12 at 14:47
  • Updated the answer to account for floats properly. It works on those three sample pages. Just to be safe: open `about:config`, search for `totalPrice` and *right-click --> Reset* that value. – Brock Adams Nov 11 '12 at 08:30
  • Just tried the updated code, and it works brilliantly!! Thanks so much for your jquery wizardry. Just curious, I may want to use this on some of my other google play scripts that are on the same domain. Is it possible to insert this code in a userscript for a different url, but same domain, and have it parse the total then prepend the value on the different page? Anyhow, thanks again for your time and brilliance. – user1774640 Nov 11 '12 at 18:45
  • You're welcome. Glad we finally got it ironed out. ... Not sure I understand your new question, but Greasemonkey can do cross-site mashups. Open a new question and give exact details of what you mean, and of the target pages. – Brock Adams Nov 11 '12 at 21:25
  • Question posted. http://stackoverflow.com/questions/13336319/can-greasemonkey-fetch-a-value-from-a-different-url-both-on-the-same-domain – user1774640 Nov 11 '12 at 22:52