Skip to content

Instantly share code, notes, and snippets.

@spudtrooper
Last active August 29, 2015 14:03
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/e6b3df99806534a47f11 to your computer and use it in GitHub Desktop.
Save spudtrooper/e6b3df99806534a47f11 to your computer and use it in GitHub Desktop.
Bookmarklet to favorite tweets on a page with a frequency of 5 seconds.
/*
* Bookmarklet to favorite tweets on a page with a frequency of 5 seconds.
*
* Usage
* =====
* Copy the link "Favorite tweets" from http://jsfiddle.net/bfCnZ/1/ to your
* toolbar and this will favorite items at a frequency of 5 seconds.
*/
(function() {
var UNFAVORITED_COLOR = 'rgb(225,232, 237)';
var PERIOD = 5 * 1000;
/** Finds one unfavorited LI and clicks it. */
function favoriteOne() {
var li = findOneLi();
if (li) {
checkFavorite(li);
}
};
/** Finds the first unfavorited LI. */
function findOneLi() {
var els = document.getElementsByTagName('li');
for (var i = 0; i < els.length; i++) {
var li = els[i];
if (li.className == 'action-fav-container js-toggle-state js-toggle-fav') {
return li;
}
}
return null;
}
/** Checks the unfavorited LI. */
function checkFavorite(li) {
var spans = li.getElementsByTagName('span');
if (spans.length != 2) {
return;
}
var favoriteSpan = spans[0];
var style = window.getComputedStyle(favoriteSpan);
var color = String(style.color).replace(' ','');
if (color == UNFAVORITED_COLOR) {
click(favoriteSpan);
}
}
/** Clicks the element. */
function click(el) {
var evt = document.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0,
false, false, false, false, 0, null);
el.dispatchEvent(evt);
}
function main() {
setInterval(favoriteOne, PERIOD);
}
main();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment