1

我有一个获取当前服务器时间的脚本,它运行良好。问题是,我想在页面上的时钟之前插入服务器时间(即使网站上的活动告诉您可以在特定的服务器时间再次执行此操作,也会显示当地时间)。出于某种原因,我无法弄清楚如何将这该死的东西粘在我想要的地方。

无需登录即可访问的目标页面是Here

我想在之前插入节点的 div 的 id 似乎是clock-wrapper

我试过的代码:(插入警报和console.log之间,在下面的“原始脚本”中)

var textNode = document.createElement('b');
var clock=document.getElementById('clock-wrapper');

textNode.appendChild ('<b>' + serverTime + '</b>');
clock.parentNode.insertBefore(textNode, clock);

var textNode = document.createElement('b');
var getRef = document.getElementById("clock-wrapper");
var parentDiv = getRef.parentNode;
parentDiv.insertBefore(textNode, getRef);


这是原始脚本

// ==UserScript==
// @name      test server time
// @namespace http://trueidiocy.us
// @include   https://www.google.com
// @include   http://stackoverflow.com
// @include   http://www.thepikaclub.co.uk*
// @require   http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @grant     GM_xmlhttpRequest
// ==/UserScript==
GM_xmlhttpRequest ( {
    url: location.href,
    method: "HEAD",
    onload: function (rsp) {
        var serverTime  = "Server date not reported!";
        var RespDate    = rsp.responseHeaders.match (/\bDate:\s+ (.+?) (?:\n|\r)/i);
        if (RespDate && RespDate.length > 1) {
            serverTime  = RespDate[1];
        }
        alert ("Server Time: " + serverTime);

        console.log ("Server Time: ", serverTime);
    }
} );


我在这里想念什么?我是否只需要找到一个比我用于未来脚本更好的参考源?另外,让脚本再次运行以每分钟左右更新一次时间有多难?

4

1 回答 1

1

The first code snippet, in the question, is invalid (bad appendChild call), but the second code snippet would work -- except that the <b> tag was added with empty contents.

Anyway, since your base script is (wisely) loading jQuery, use it. jQuery's .before() works great here. The code would be:

GM_xmlhttpRequest ( {
    url:    location.href,
    method: "HEAD",
    onload: function (rsp) {
        var serverTime  = "Server date not reported!";
        var RespDate    = rsp.responseHeaders.match (/\bDate:\s+(.+?)(?:\n|\r)/i);
        if (RespDate  &&  RespDate.length > 1) {
            serverTime  = RespDate[1];
        }
        $("#clock-wrapper").before ('<b>' + serverTime + '</b>');
    }
} );

If you want to re-fetch the server time every minute, or so, use javascript's setInterval(). Except that you need to avoid creating more than one <b> tag, so the code needs an additional check. It becomes:

getServerTime (); //-- Initial call

//-- Recheck every minute
var clkRefreshTimer = setInterval (getServerTime, 60 * 1000); // 60 seconds

function getServerTime () {
    GM_xmlhttpRequest ( {
        url:    location.href,
        method: "HEAD",
        onload: function (rsp) {
            var serverTime  = "Server date not reported!";
            var RespDate    = rsp.responseHeaders.match (/\bDate:\s+(.+?)(?:\n|\r)/i);
            if (RespDate  &&  RespDate.length > 1) {
                serverTime  = RespDate[1];
            }

            //-- Does our display node already exist?
            var ourTimeDisp = $("#gmTimeDisplay");
            if (ourTimeDisp.length) {
                ourTimeDisp.text (serverTime);
            }
            else {
                $("#clock-wrapper").before (
                    '<b id="gmTimeDisplay">' + serverTime + '</b>'
                );
            }
        }
    } );
}

However, you might be smart to look up how to add a JS clock to the page and only call GM_xmlhttpRequest once just to initialize your new clock. Then your clock JS would update it as often as you liked without aggravating the server.

于 2013-10-09T07:53:41.623 回答