Skip to content

Instantly share code, notes, and snippets.

@runspired
Created September 7, 2012 03:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save runspired/3662827 to your computer and use it in GitHub Desktop.
Save runspired/3662827 to your computer and use it in GitHub Desktop.
Twitter Timeline Manager
/*
This javascript closure will create a timeline manager to let you filter
out users and keywords from your stream. In theory it stores the info
in a cookie for one day, but cookie setting appears to be not working currently.
The manager object has methods for showing a hidden username or keyword,
but currently no buttons to do so.
Keyword filtration is case sensitive and is done based on a single string
match on the tweet's content, it does not match usernames that might
contain the keyword.
User filtration is case sensitive and is done based on the user's handle
as Twitter renders it. This means that J_aCAPShere is different from j_AcapsHERE.
You need to enter the username as Twitter renders it for the filtration to work.
Its possible you will still momentarily see a tweet just after it loads,
tweets are filtered every .1 seconds.
This will not work on any browser that does not have the html5 "hidden" method.
This can easily be updated to toggle display between "none" and "block"
to be more widely useable.
This is just a tiny project I will occasionally add to, because I often use
twitter.com but rarely want to read the tweets of some of the current topics
that are popular. As opposed to unfollowing these people permanently, this
just removes their tweets from your timeline while you are running the code.
I will eventually make this a bookmark app. :)
*/
(function(){
function Manager() {
var tm = this;
var timelineOptions = document.createElement('div');
timelineOptions.style.cssText = "position: fixed; z-index: 999; top: 40px; left: 0px; width: 315px; min-height: 500px; height: 100%; background: rgba(0,0,0,0.9);";
var html = '<p><input type="text" class="timelineManager-input" name="timelineManager-hideUser"/><input type="button" onclick="timelineManager.hideTweetsFromScreenName(document.querySelector(\'[name=timelineManager-hideUser]\').value);" value="hide user"/></p>';
html += '<p><input type="text" class="timelineManager-input" name="timelineManager-hideKeyword"/><input type="button" onclick="timelineManager.hideTweetsWithKeyword(document.querySelector(\'[name=timelineManager-hideKeyword]\').value);" value="hide keyword"/></p>';
timelineOptions.innerHTML = html;
var hiddenUsers = new Object();
var hiddenKeywords = new Object();
function setCookie(c_name,value,exdays) {
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
};
function getCookie(c_name) {
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
};
function deleteFromCookie(c_name, s) {
var c_val = getCookie(c_name);
if (c_val!=null && c_val!="" && c_val.indexOf(s) !== -1) {
c_val = c_val.split(',');
var index = c_val.length,
n_val = new Array();
while(index--) {
if(c_val[index] != s)
n_val.push(c_val[index]);
}
setCookie(c_name,n_val.join(','),1);
}
};
function addToCookie(c_name,s) {
var c_val = getCookie(c_name);
if (c_val!=null && c_val!="") {
c_val = c_val.split(',');
var index = c_val.length;
while(index--) {
if(c_val[index] == s)
return;
}
c_val.push(s);
setCookie(c_name,c_val.join(','),1);
}
};
function hideTweetsUserInterval(s) {
var tweets = document.querySelectorAll('[data-screen-name='+s+']');
var length = tweets.length;
while(length--)
tweets[length].parentNode.hidden = true;
};
function hideTweetsKeywordInterval(s) {
var tweets = document.getElementsByClassName('stream-item');
var length = tweets.length;
while(length--) {
var text = tweets[length].querySelectorAll('.content')[0].querySelectorAll('.js-tweet-text')[0].innerText;
if(text.indexOf(s) !== -1)
tweets[length].hidden = true;
}
};
tm.hideTweetsFromScreenName = function(s) {
if(s.indexOf('@') !== -1)
s = s.replace('@','');
addToCookie("timelineManager-users",s);
hiddenUsers[s] = setInterval((function(){ hideTweetsUserInterval(s);}),100);
};
tm.showTweetsFromScreenName = function(s) {
if(s.indexOf('@') !== -1)
s = s.replace('@','');
deleteFromCookie("timelineManager-users",s);
clearInterval( hiddenUsers[s] );
var tweets = document.querySelectorAll('[data-screen-name='+s+']');
var length = tweets.length;
while(length--)
tweets[length].parentNode.hidden = false;
};
tm.showTweetsWithKeyword = function(s) {
deleteFromCookie("timelineManager-keywords",s);
clearInterval( hiddenUsers[s] );
var tweets = document.getElementsByClassName('stream-item');
var length = tweets.length;
while(length--) {
var content = tweets[length].querySelectorAll('.content');
var text = '';
if(content.length > 0)
text = tweets[length].querySelectorAll('.content')[0].querySelectorAll('.js-tweet-text')[0].innerText;
if(text.indexOf(s) !== -1)
tweets[length].hidden = false;
}
};
tm.hideTweetsWithKeyword = function(s) {
addToCookie("timelineManager-keywords",s);
hiddenUsers[s] = setInterval((function(){ hideTweetsKeywordInterval(s);}),100);
};
function setup() {
var keywords = getCookie("timelineManager-keywords");
if (keywords!=null && keywords!="") {
keywords = keywords.split(',');
var index = keywords.length;
while(index--) {
tm.hideTweetsWithKeyword(keywords[index]);
}
}
var users = getCookie("timelineManager-users");
if (users!=null && users!="") {
users = users.split(',');
var index = users.length;
while(index--) {
tm.hideTweetsWithuser(users[index]);
}
}
};
setup();
document.body.appendChild(timelineOptions);
};
window.timelineManager = new Manager();
}).call(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment