如何在Greasemonkey中解析外部JSON代码并使用比较运算符?

如何在Greasemonkey中解析外部JSON代码并使用比较运算符?,json,greasemonkey,Json,Greasemonkey,我想解析这个JSON提要,然后在JSON提要中“total”的新实例大于例如100时执行一个alert()。最简单的方法是什么?这是我目前掌握的代码 我想做的本质是,理想情况下,我希望它能播放声音文件并向我发送电子邮件,因为我知道从浏览器执行外部宏是不可能的: $.getJSON('https://api.mintpal.com/v1/market/trades/bc/btc', {}, function(JsonData){ if ( total >= 10 ) {

我想解析这个JSON提要,然后在JSON提要中“total”的新实例大于例如100时执行一个alert()。最简单的方法是什么?这是我目前掌握的代码

我想做的本质是,理想情况下,我希望它能播放声音文件并向我发送电子邮件,因为我知道从浏览器执行外部宏是不可能的:

 $.getJSON('https://api.mintpal.com/v1/market/trades/bc/btc', {}, function(JsonData){

    if ( total >= 10 ) {
      alert(A trade with a value over 10 BTC has been executed.)
    }

// some code to reload the feed to check for new data every 60 seconds.
全部内容:

// ==UserScript==
// @name        Mintpal1
// @namespace   MintPal
// @include     https://api.mintpal.com/v1/market/trades/bc/btc
// @version     1
// @grant       none
// ==/UserScript==

var $;

// Add jQuery
    (function(){
        if (typeof unsafeWindow.jQuery == 'undefined') {
            var GM_Head = document.getElementsByTagName('head')[0] || document.documentElement,
                GM_JQ = document.createElement('script');

            GM_JQ.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';
            GM_JQ.type = 'text/javascript';
            GM_JQ.async = true;

            GM_Head.insertBefore(GM_JQ, GM_Head.firstChild);
        }
        GM_wait();
    })();

// Check if jQuery's loaded
    function GM_wait() {
        if (typeof unsafeWindow.jQuery == 'undefined') {
            window.setTimeout(GM_wait, 100);
        } else {
            $ = unsafeWindow.jQuery.noConflict(true);
            letsJQuery();
        }
    }

// All your GM code must be inside this function
    function letsJQuery() {
        alert($); // check if the dollar (jquery) function works
        alert($().jquery); // check jQuery version
    }

$.getJSON('https://api.mintpal.com/v1/market/trades/bc/btc', {}, function(JsonData){

    if ( total >= 10 ) {
      alert(A trade with a value over 10 BTC has been executed.)
    }


  }

});

<script>
     var time = new Date().getTime();
     $(document.body).bind("mousemove keypress", function(e) {
         time = new Date().getTime();
     });

     function refresh() {
         if(new Date().getTime() - time >= 60000) 
             window.location.reload(true);
         else 
             setTimeout(refresh, 10000);
     }

     setTimeout(refresh, 10000);
</script>
/==UserScript==
//@name Mintpal1
//@MintPal
//@包括https://api.mintpal.com/v1/market/trades/bc/btc
//@version 1
//@grant none
//==/UserScript==
var$;
//添加jQuery
(功能(){
if(typeof unsafeWindow.jQuery==“未定义”){
var GM|u Head=document.getElementsByTagName('Head')[0]| | document.documentElement,
GM_JQ=document.createElement('script');
GM_JQ.src=http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';
GM_JQ.type='text/javascript';
GM_JQ.async=true;
GM_Head.insertBefore(GM_JQ,GM_Head.firstChild);
}
等等;
})();
//检查jQuery是否已加载
函数GM_wait(){
if(typeof unsafeWindow.jQuery==“未定义”){
设置超时(GM_等待,100);
}否则{
$=unsafeWindow.jQuery.noConflict(true);
letsJQuery();
}
}
//您的所有GM代码必须在此功能内
函数letsJQuery(){
警报($);//检查美元(jquery)函数是否工作
警报($().jquery);//检查jquery版本
}
$.getJSON('https://api.mintpal.com/v1/market/trades/bc/btc',{},函数(JsonData){
如果(总数>=10){
警报(已执行价值超过10 BTC的交易。)
}
}
});
var time=new Date().getTime();
$(document.body).bind(“mousemove按键”,函数(e){
时间=新日期().getTime();
});
函数刷新(){
如果(新日期().getTime()-时间>=60000)
window.location.reload(true);
其他的
设置超时(刷新,10000);
}
设置超时(刷新,10000);

使用$.getJSON时,您不必解析JSON响应,它将自动完成

但最终生成的JSON包含一个
交易数组
。如果某个交易的
total
属性大于100,则必须循环此数组并执行警报(或任何您想要的代码)

$.getJSON('https://api.mintpal.com/v1/market/trades/bc/btc', {}, function(JsonData){
    var trades = JsonData.trades;
    for(i=0, max=trades.length; i<max; i++) {
       if ( trades[i].total >= 100 ) {
         alert("A trade with a value over 100 BTC has been executed.")
       }
    }

});
$.getJSON('https://api.mintpal.com/v1/market/trades/bc/btc',{},函数(JsonData){
var交易=JsonData.trades;
对于(i=0,max=trades.length;i=100){
警报(“已执行价值超过100 BTC的交易。”)
}
}
});