Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mollifier/572201 to your computer and use it in GitHub Desktop.
Save mollifier/572201 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name twitter-average-length2
// @namespace http://www.hatena.ne.jp/mollifier
// @include http://twitter.com/*
// @include https://twitter.com/*
// ==/UserScript==
// Fork of twitter-average-length.user.js (http://gist.github.com/402749)
// original author is hitode909 (http://gist.github.com/hitode909)
// 日付文字列のパース処理に eval を使用しないように変更
// RT の場合に RT 元の発言時刻ではなく RT した時刻を取得するように変更
(function() {
var header = document.querySelector('h2.thumb');
if (!header) return;
// length
var average = 0;
var entries = document.querySelectorAll('.entry-content');
Array.forEach(entries, function(entry) {
average += entry.textContent.length;
});
average /= entries.length;
div = document.createElement('div');
div.textContent = '平均' + Math.floor(average * 100) / 100 + '文字';
header.appendChild(div);
// {time:'Tue Aug 31 18:47:17 +0000 2010'} という形式の文字列から
// Date オブジェクトのミリ秒形式の値を取得する
var getTimeFromDateString = function(dateString) {
var ret = null;
var re = /time\s*:\s*(['"])(.+?)\1/;
var ma = dateString.match(re);
if (ma !== null && ma.length >= 3) {
ret = Date.parse(ma[2]) || null;
}
return ret;
};
// 1つのつぶやきを含む HTML 要素から
// {time:'Tue Aug 31 18:47:17 +0000 2010'} という形式の日付文字列を取得する
var getDateDate = function(statusBody) {
var ret = "";
var timestamps = null;
var rts = statusBody.querySelectorAll('.retweet-meta');
if (rts !== null && rts.length > 0) {
// RT の場合
timestamps = rts[0].querySelectorAll('.timestamp-title');
} else {
// RT でない場合
timestamps = statusBody.querySelectorAll('.timestamp');
}
if (timestamps !== null && timestamps.length > 0) {
ret = timestamps[0].getAttribute("data")
}
return ret;
};
// speed
var statusBodies = document.querySelectorAll('.status-body');
var topTime = getTimeFromDateString(getDateDate(statusBodies[0]));
var bottomTime = getTimeFromDateString(getDateDate(statusBodies[statusBodies.length - 1]));
if (! topTime || ! bottomTime) {
// パースエラー
return;
}
var per_tweet = (topTime - bottomTime) / statusBodies.length / 1000;
var maps = [ {size: 60 * 60 * 24, name: "日"}, {size: 60 * 60, name: "時間"}, {size:60, name: "分"}, {size:1, name: "秒"}];
var map;
for(var i = 0; i < maps.length; i++) with({m: maps[i]}) {
console.log([m, per_tweet]);
if (m.size < per_tweet) {
map = m;
break;
}
}
if (!map) map = maps[maps.length - 1];
div = document.createElement('div');
div.textContent = Math.floor((per_tweet / map.size) * 100) / 100 + map.name + 'に1回投稿';
header.appendChild(div);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment