Javascript Greasemonkey能否从URL';s
我想从Javascript Greasemonkey能否从URL';s,javascript,jquery,greasemonkey,mashup,Javascript,Jquery,Greasemonkey,Mashup,我想从https://play.google.com/store/account*,它使用户页面通过其输出。例如: /store/account?start=0&num=40,然后/store/account?start=40&num=40,等等 现在,当我访问https://play.google.com/apps,我希望Greasemonkey将/store/account页面中的值相加,然后在该页面上显示最终值 下面列出的代码可以从/store/account页面合计我想要的值。但是,我想
https://play.google.com/store/account*
,它使用户页面通过其输出。例如:/store/account?start=0&num=40
,然后/store/account?start=40&num=40
,等等
现在,当我访问https://play.google.com/apps
,我希望Greasemonkey将/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);
}
}
/==UserScript==
//@name谷歌游戏
//@需要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(启动图){
var totalPrice=0;
var startNum=parseInt(startParam[1],10);
如果(startNum==0){
GM_设定值(“总价”、“0”);
}
否则{
totalPrice=parseFloat(GM_getValue(“totalPrice”,0));
}
$(“#tab body account.rap link”)。每个(函数(){
var price=$(this.attr(“数据docprice”).replace(/[^\d\.]/g,”);
如果(价格){
价格=浮动(价格);
如果(价格类型==“数量”){
总价+=价格;
}
}
} );
//console.log(“totalPrice:,totalPrice.toFixed(2));
$(“.选项卡式面板选项卡”)。之前(
“*组合值:$”+总价。toFixed(2)+”
);
GM_设定值(“总价格”,“总价格”);
if($(“.snippet.snippet-tiny”).length){
startNum+=40;
var nextPage=location.href.replace(
/\b开始=\d+/i,“开始=”+startNum
);
地点分配(下一页);
}
}
从页面/站点获取mashup数据的基本方法有:
这几乎适用于所有页面,但不适用于通过AJAX加载所需内容的页面。有时,对于需要身份验证或限制推荐人的网站,这也会变得棘手。
用于大多数情况,以允许跨域脚本编写。下面将详细介绍这种方法
中加载资源页:这种方法适用于AJAX页面,可以对其进行编码,让用户手动处理登录问题。但是,这是:更慢,资源更密集,代码更复杂 由于此问题的细节似乎不需要它,请参阅以获取有关此技术的更多信息
唉,大多数网站都没有API,所以这可能不是您的选择,但值得确保没有提供API。如果API可用,它通常是最好的方法。有关此方法的更多详细信息,请执行新的搜索/提问
此选项也不适用于大多数站点,但在需要时,它可以是一种干净、高效的技术。有关此方法的更多详细信息,请执行新的搜索/提问
通过支持跨域的AJAX从一系列网页中获取值: 使用
GM\u 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 + '! '
+ '"' + respObject.statusText + '" '
+ '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;
}
/==UserScript==
//@name\u总值mashup
//@包括https://play.google.com/apps*
//@需要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;
//---在第一个帐户页中删除项目值:
$(“正文”)。前缀(
'正在获取总值,请稍候…'
);
第页();
函数页(){
var accntPage=https://play.google.com/store/account?start=0&num=40';
accntPage=accntPage.replace(/start=\d+/i,“start=“+startNum”);
$(“#gm#U状态栏”)。附加(
'正在获取页面'+accntPage+'…'
);
GMxmlHttpRequest({
方法:“GET”,
url:accntPage,
//---getTotalValuesFromPage()还会根据需要获取下一页。
onload:getTotalValuesFromPage,
onabort:reportAJAX\u错误,
onerror:reportAJAX\u错误,
ontimeout:reportAJAX\u错误
} );
}
函数getTotalValuesFromPage(respObject){
if(respObject.status!=200&&respObject.status!=304){
报告AJAX_错误(respObject);
返回;
}
$(“#gm#U状态栏”).append('done');
var respDoc=$(respObject.responseText);
var targetElems=respDoc.find(“#tab body account.rap link”);
targetElems.each(函数(){
var itmVal=$(this).attr(“数据文档价格”).replace(/[^\d\.]/g,”);
如果(itmVal){
itmVal=parseFloat(itmVal);
if(itmVal的类型==“编号”){
totalValue+=itmVal;
}
}
} );
console.log(“totalValue:”,totalValue.to