问题
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.
回答1:
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==
来源:https://stackoverflow.com/questions/13221698/increment-value-within-a-url-in-order-to-parse-data-from-attributes-that-are-pag