-1
<abbr title="Sunday, January 01, 2017 at 12:12am" 
data-utime="0000000000" data-shorten="1" 
class="_5ptz timestamp livetimestamp">
<span class="timestampContent" id="js_d">1 hr</span></abbr>

我找到了一个脚本,但它不再有效。那么,这里有人愿意分享如何做到这一点吗?

document.addEventListener("load", DisplayTimestamp());

function DisplayTimestamp() {
    var i;

    if (document.getElementsByTagName && document.getElementsByTagName("abbr").length > 0) {
        for (i = 0; i < document.getElementsByTagName("abbr").length; i++) {
            document.getElementsByTagName("abbr")[i].innerHTML += " 
    (" + new Date(document.getElementsByTagName("abbr")[i].title).toLocaleString() + ")";
        }
    }
}

到目前为止,尽管我不懂 javascript,但我已经取得了进展。

我发现,

document.getElementsByTagName("abbr").title;
document.getElementsByTagName("abbr")[i].innerHTML;       
document.getElementsByTagName("abbr")[i].title;

实际上检索标题并将其输出到控制台中。虽然不知道从现在开始该怎么做。

进一步的进展,我已经设法让它从控制台对实际页面进行更改。

var i = 0;
var d = new Date(); 
var n = d.toLocaleString();
document.getElementsByTagName("abbr")[i].title;
document.getElementsByTagName("abbr")[i].innerHTML;       
document.getElementsByTagName("abbr")[i].title;
document.getElementsByTagName("abbr")[i].innerHTML = n;

我不知道我需要做什么才能在脚本中工作并始终显示时间戳,此时我可能会制作一个按钮,尽管单击时会显示时间戳,然后将其构建到浏览器中扩大。

如果我希望它以 word 格式显示实际的日期和月份,

var i = 0;
var n = document.getElementsByTagName("abbr")[i].title.toLocaleString();
document.getElementsByTagName("abbr")[i].title;
document.getElementsByTagName("abbr")[i].innerHTML;
document.getElementsByTagName("abbr")[i].title;
document.getElementsByTagName("abbr")[i].innerHTML = n;

进一步进展:

(function DisplayTimestamp() {
    var i;
    var abbrs = document.getElementsByTagName("abbr")
    if (document.getElementsByTagName && abbrs.length > 0) {
        for (i = 0; i < abbrs.length; i++) {
            abbrs[i].innerHTML += "(" + abbrs[i].title.toLocaleString() + ")";
            //console.log(document.getElementsByTagName("abbr")[i].title.toLocaleString())
        }
    }
})()
4

1 回答 1

0

使用data-utime属性来构造日期,所以假设abbr是元素引用:

new Date(abbr.dataset.utime * 1000).toLocaleString()

但是,由于 Facebook 动态加载内容,因此最好使用 MutationObserver 来处理页面加载后添加的帖子(滚动时也是如此)。这也将帮助我们通过添加以下内容使时间戳出现没有任何延迟@run-at document-start

// ==UserScript==
// @name     FB: full timestamps
// @match    *://www.facebook.com/*
// @run-at   document-start
// @grant    GM_addStyle
// @require  https://greasyfork.org/scripts/12228/code/setMutationHandler.js
// ==/UserScript==

GM_addStyle(
    '.full-timestamp { opacity: 0.5; }' +
    '.full-timestamp:hover { opacity: 1.0; }' +
    '.full-timestamp:before { content: " ("; }' +
    '.full-timestamp:after  { content: ")"; }'
);

// process the already loaded portion of the page if any
expandDates(document.querySelectorAll('abbr[data-utime]'));

// process the stuff added from now on
setMutationHandler(document, 'abbr[data-utime]', expandDates);

function expandDates(nodes) {
    for (var i = 0, abbr; (abbr = nodes[i++]); ) {
        if (abbr.querySelector('.full-timestamp')) {
            // already processed
            continue;
        }
        abbr.insertAdjacentHTML('beforeend', '<span class="full-timestamp">' +
            new Date(abbr.dataset.utime * 1000).toLocaleString() + '</span>');
    }
}

该代码使用简单的 MutationObserver 包装器setMutationHandler

于 2017-01-15T17:42:56.020 回答