Skip to content

Instantly share code, notes, and snippets.

@spudtrooper
Created March 13, 2012 12:53
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 spudtrooper/2028580 to your computer and use it in GitHub Desktop.
Save spudtrooper/2028580 to your computer and use it in GitHub Desktop.
Only display twitter updates in batches of certain numbers. By default the minimum number of item required to show is 10, this can be changed via localStorage to, say, N, e.g. localStorage.setItem('batch.number',N)
// ==UserScript==
// @name Twitter Batch
// @namespace http://jeffpalm.com/twitterbatch
// @description Only display twitter updates in batches of certain numbers.
// By default the minimum number of item required to show is
// 10, this can be changed via localStorage to, say, N, e.g.
// localStorage.setItem('batch.number',N)
// @include https://twitter.com
// @include https://www.twitter.com
// @include https://twitter.com/*
// @include https://www.twitter.com/*
// ==/UserScript==
(function() {
const BATCH_NUMBER_KEY = 'batch.number';
const BATCH_NUMBER_DEFAULT = 10;
function note(msg) {
try {
console.log(msg);
} catch (_) {}
}
function getBatchNumber() {
var key = BATCH_NUMBER_KEY;
var res = BATCH_NUMBER_DEFAULT;
try {
var num = parseInt(localStorage.getItem(key));
if (!isNaN(num) && num > 0) res = num;
} catch (_) {}
return res;
}
function Checker() {
this.ignoreNextChange = false;
this.hideNumTweetsSignTries = 0;
}
const HIDE_NUM_TWEETS_TRIES = 5;
Checker.prototype = {
hideNumTweetsSign: function() {
var removed = false;
var divs = document.getElementsByTagName('DIV');
for (var i in divs) {
var div = divs[i];
if (!!div.className && div.className.match(/new-tweets-bar/)) {
div.parentNode.removeChild(div);
removed = true;
break;
}
}
if (!removed) {
if (this.hideNumTweetsSignTries-- > 0) {
var thiz = this;
setTimeout(function(){thiz.hideNumTweetsSign();},500);
}
}
},
checkNumTweets: function() {
if (this.ignoreNextChange) {
this.ignoreNextChange = false;
return;
}
var title = String(document.title);
var total = 0;
var res;
if (res = title.match(/\(([\d,]+)\)/)) {
total = parseInt(res[1].replace(/\D/g,''));
}
var min = getBatchNumber();
note('checkNumTweets: total='+total + ',min='+min);
if (total == 0) return;
if (total < min) {
this.ignoreNextChange = true;
document.title = title.replace(/\s*\([\d,]+\)\s*/g,'');
this.hideNumTweetsSignTries = HIDE_NUM_TWEETS_TRIES;
this.hideNumTweetsSign();
}
}
};
function main() {
var titleEl = document.getElementsByTagName('title')[0];
var docEl = document.documentElement;
var c = new Checker();
if (docEl && docEl.addEventListener) {
docEl.addEventListener('DOMSubtreeModified', function(evt) {
var t = evt.target;
if (t === titleEl ||
(t.parentNode && t.parentNode === titleEl)) {
c.checkNumTweets();
}
}, false);
} else {
document.onpropertychange = function() {
if (window.event.propertyName == 'title') {
c.checkNumTweets();
}
};
}
c.checkNumTweets();
}
main();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment