2012-11-11 35 views
4

我想從https://play.google.com/store/account*中獲取一個值,它使用戶頁面通過它的輸出。例如:
/store/account?start=0&num=40,然後/store/account?start=40&num=40Greasemonkey可以從分頁的URL序列中獲取值嗎?

現在,當我訪問https://play.google.com/apps,我想向Greasemonkey的TOT-了從/store/account頁面的值,然後顯示該網頁上的最終值。

下面列出的代碼可以從/store/account頁面獲得我想要的值。但是,我想將代碼插入用於第二個URL的腳本中,因此我可以在同一頁面上添加它。

// ==UserScript== 
// @name  Google Play 
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js 
// @grant  GM_setValue 
// @grant  GM_getValue 
// ==/UserScript== 

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 ($(".snippet.snippet-tiny").length) { 
      startNum  += 40; 
      var nextPage = location.href.replace (
       /\bstart=\d+/i, "start=" + startNum 
      ); 
      location.assign (nextPage); 
     } 
    } 

回答

9

從一個網頁/網站獲得數據混搭的基本方法是:

  1. 通過AJAX抄襲:
    這適用於幾乎所有的網頁,雖然它不會使用通過AJAX加載所需內容的頁面。偶爾,對於需要認證或限制推薦人的網站也會變得棘手。
    對於大多數情況使用GM_xmlhttpRequest(),以允許跨域腳本。這種方法將在下面詳述。

  2. 在加載資源頁面(S):
    這種方法適用於AJAX-指明分數的網頁,並且可以被編碼,讓用戶處理登錄手動問題。但是,這是:更慢,更耗費資源,編碼更復雜。

    由於此問題的詳細資料似乎不需要,因此有關此技術的更多信息,請參閱"How to get an AJAX get-request to wait for the page to be rendered before returning a response?"

  3. 使用該網站的API,如果它有一個:
    唉,大部分網站沒有一個API,所以這可能不是一個選擇,但它是值得確保沒有API是提供。如果API可用,API通常是最好的方法。做一個新的搜索/問題關於這種方法的更多細節。

  4. 模仿網站的AJAX調用,如果它使這種呼籲的那種信息,你想:
    此選項也不適用於大多數的網站,但它可以是一種清潔,高效的技術,當它是。做一個新的搜索/問題關於這種方法的更多細節。通過跨域能力的AJAX從網頁中的一個序列


擷取值(S):

使用GM_xmlhttpRequest()加載網頁,和jQuery處理其HTML。
使用GM_xmlhttpRequest()onload函數調用下一頁,如有必要,不要嘗試使用同步AJAX調用。

的核心邏輯,從原來的劇本,移動到onload函數內 - 除了有不再需要記住值之間的Greasemonkey運行。

這裏的一個完整的Greasemonkey腳本,在投入了一些狀態和錯誤報告:

// ==UserScript== 
// @name  _Total-value mashup 
// @include  https://play.google.com/apps* 
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js 
// @grant  GM_addStyle 
// @grant  GM_xmlhttpRequest 
// ==/UserScript== 

var startNum  = 0; 
var totalValue  = 0; 

//--- Scrape the first account-page for item values: 
$("body").prepend (
    '<div id="gm_statusBar">Fetching total value, please wait...</div>' 
); 
scrapeAccountPage(); 

function scrapeAccountPage() { 
    var accntPage = 'https://play.google.com/store/account?start=0&num=40'; 
    accntPage  = accntPage.replace (/start=\d+/i, "start=" + startNum); 

    $("#gm_statusBar").append (
     '<span class="gmStatStart">Fetching page ' + accntPage + '...</span>' 
    ); 

    GM_xmlhttpRequest ({ 
     method:  'GET', 
     url:  accntPage, 
     //--- getTotalValuesFromPage() also gets the next page, as appropriate. 
     onload:  getTotalValuesFromPage, 
     onabort: reportAJAX_Error, 
     onerror: reportAJAX_Error, 
     ontimeout: reportAJAX_Error 
    }); 
} 

function getTotalValuesFromPage (respObject) { 
    if (respObject.status != 200 && respObject.status != 304) { 
     reportAJAX_Error (respObject); 
     return; 
    } 

    $("#gm_statusBar").append ('<span class="gmStatFinish">done.</span>'); 

    var respDoc  = $(respObject.responseText); 
    var targetElems = respDoc.find ("#tab-body-account .rap-link"); 

    targetElems.each (function() { 
     var itmVal = $(this).attr ("data-docprice").replace (/[^\d\.]/g, ""); 
     if (itmVal) { 
      itmVal = parseFloat (itmVal); 
      if (typeof itmVal === "number") { 
       totalValue += itmVal; 
      } 
     } 
    }); 
    console.log ("totalValue: ", totalValue.toFixed(2)); 

    if (respDoc.find (".snippet.snippet-tiny").length) { 
     startNum  += 40; 
     //--- Scrape the next page. 
     scrapeAccountPage(); 
    } 
    else { 
     //--- All done! report the total. 
     $("#gm_statusBar").empty().append (
      'Combined Value: $' + totalValue.toFixed(2) 
     ); 
    } 
} 

function reportAJAX_Error (respObject) { 
    $("#gm_statusBar").append (
     '<span class="gmStatError">Error ' + respObject.status + '! &nbsp; ' 
     + '"' + respObject.statusText + '" &nbsp; &nbsp;' 
     + 'Total value, so far, was: ' + totalValue 
     + '</span>' 
    ); 
} 

//--- Make it look "purty". 
GM_addStyle (multilineStr (function() {/*! 
    #gm_statusBar { 
     margin:   0; 
     padding:  1.2ex; 
     font-family: trebuchet ms,arial,sans-serif; 
     font-size:  18px; 
     border:   3px double gray; 
     border-radius: 1ex; 
     box-shadow:  1ex 1ex 1ex gray; 
     color:   black; 
     background:  lightgoldenrodyellow; 
    } 
    #gm_statusBar .gmStatStart { 
     font-size:  0.5em; 
     margin-left: 3em; 
    } 
    #gm_statusBar .gmStatFinish { 
     font-size:  0.5em; 
     background:  lime; 
    } 
    #gm_statusBar .gmStatError { 
     background:  red; 
     white-space: nowrap; 
    } 
*/})); 

function multilineStr (dummyFunc) { 
    var str = dummyFunc.toString(); 
    str  = str.replace (/^[^\/]+\/\*!?/, '') // Strip function() { /*! 
      .replace (/\s*\*\/\s*\}\s*$/, '') // Strip */ } 
      .replace (/\/\/.+$/gm, '') // Double-slash comments wreck CSS. Strip them. 
      ; 
    return str; 
} 



重要:不要忘了@include@exclude和/或@match指令,因此您的腳本不會在每個頁面和iframe上運行!

+1

哇,這工作完美無瑕。再次感謝Brock! – user1774640

+0

不客氣。樂意效勞。 –

相關問題