Main Page: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
(Reverting back to pre-vandalism. (Signed up just to fix this so I could use the page lol))
No edit summary
Line 1: Line 1:
__NOTOC__
// ==UserScript==
'''GreaseSpot''' is community documentation for [[user script]]ing with [[Greasemonkey]]. Please [[GreaseSpot:Editor portal|contribute]].
// @name          Facebook Online Logger
// @namespace      FBOnline
// @include        http://*.facebook.com*
// @exclude        http://*.facebook.com/login.php*
// @exclude        http://en-gb.facebook.com/*
// @description    Log out of Facebook Chat and still see who is online.
// @author    http://userscripts.org/users/59449
// @version    3.3
// ==/UserScript==


== Getting Started ==
// Modified version of
Before writing a script or posting to the [[mailing list]] for help, '''every''' script writer should read Mark Pilgrim's [http://commons.oreilly.com/wiki/index.php/Greasemonkey_Hacks/Getting_Started#Avoid_Common_Pitfalls Avoid Common Pitfalls in Greasemonkey].
// http://userscripts.org/scripts/show/19450
// http://userscripts.org/scripts/show/23499


* [[Greasemonkey Manual]]
// Ad remover
* [[Tutorials]]
window.addEventListener("load", function(e) {
* [[Mailing list]]
var elements = xpath("//div[contains(@class, 'ad_capsule')] | //div[contains(@class, 'social_ad')] | //div[@id='announce'] | //div[contains(@id, 'sponsor')] | //div[contains(@id, 'ssponsor')]");
if (elements.snapshotLength > 0) {
for (var i = 0; i < elements.snapshotLength; i++) {
var thisElement = elements.snapshotItem(i);
thisElement.parentNode.removeChild(thisElement);
}
}
}, false);
function xpath (query) {
return document.evaluate(query, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
}


== Scripting References ==
init();
function init() {
if (!document.body) { window.setTimeout(function(){init();}, 300); }
if (window != window.top) return;  // Don't run in a frame
if ($('loginform')) return; // Don't run if not logged in
MenuCommands();
if (GM_getValue('ShowPopUp', true)) { createChatMenu(); }
facebookOnline();
}


* [[API reference]]
function facebookOnline() {
* [[Metadata block]]
GM_xmlhttpRequest({
* [[:Category:Scripting context|Scripting context]]
method:"GET",
** [[Sandbox]]
url:"http://iphone.facebook.com/friends.php?v=online",
** [[Global object]]
headers:{"User-Agent":"Mozilla/5.0","Accept":"text/xml"},
** [[XPCNativeWrapper]]
onload:function(response) {
** [[Scriptable content]]
if((response.readyState == 4) && (response.status == 200))
** [[DOMContentLoaded]]
parseResults(response.responseText);
* [[Security]]
}
** [[unsafeWindow]]
});


== Authoring Tips ==
window.setTimeout(facebookOnline, 60000); // Refresh every 60 seconds
* [[Useful Tools for Script Writers]]
}
* [[Troubleshooting]]
* [[0.7.20080121.0+ compatibility]]: Working around errors introduced by changes in Greasemonkey 0.7.20080121.0
* [[Controlling userscripts in sub windows]]
* [[Code snippets]]: useful staple functions.
* [[Coding tips]]: things you might not have thought of.
* [[HTML injection tips]]: how to add your html anywhere, dodging page CSS invading yours
* [[:Category:Site-specific tips|Site-specific tips]]
** [[:Category:Ajax site tips|Ajax site tips]]
** [[Gmail]]
* [[Security tips]]
* [[Etiquette]]
* [[Cross-browser userscripting]]


== Extension Development ==
function parseResults(temp) {
var rt = process(temp);


* [http://greasemonkey.devjavu.com/projects/greasemonkey/wiki/ContributeToGreasemonkey Contributing to Greasemonkey ]
var uLINK = "";
var popText = "";
var today = new Date();
var h = today.getHours();
if (h<10) h = "0"+h;
var m = today.getMinutes();
if (m<10) m = "0"+m;
var dd = today.getDate();
var mm = today.getMonth()+1;
var yy = today.getFullYear();
var t = dd+"/"+mm+"/"+yy+" "+h+":"+m;


== Also ==
var lastchecked = GM_getValue("lastupdated", t);
for (var i=0; i<rt.length;i+=1) {
var uID = getUID(rt[i]);
var uNAME = getUName(rt[i]);
var a = GM_getValue(uID,uNAME+","+t+","+t);
var b = a.split(",");
var c;
if (lastchecked == b[2]) {
c = b[1].split(" ");
GM_setValue(uID,uNAME+","+b[1]+","+t);  //was online
}
else {
c = t.split(" ");
GM_setValue(uID,uNAME+","+t+","+t);  //now online
}
if (GM_getValue('ShowTime', true)) {
uLINK = c[1] + ' <a href="http://www.facebook.com/profile.php?id='+uID+'">'+uNAME+'</a>';
}
else {
uLINK = '<a href="http://www.facebook.com/profile.php?id='+uID+'">'+uNAME+'</a>';
}
popText += '<div id="fbmLINK">'+uLINK+'</div>'; // For popup menu - add hh:mm here
}


* [[FAQ]]
if (GM_getValue('ShowPopUp', true)) { updatePopUp(popText,rt.length); } // Popup menu in place of chat
* [[Links]]
GM_setValue("lastupdated", t);
}
 
// Adds the online friends list on top of the Facebook Chat bar
function createChatMenu (){
var onshow = GM_getValue('ShowHide', false);
if (onshow != true && onshow != false) {
onshow = false;
GM_setValue('ShowHide', onshow);
}
 
GM_addStyle(
'#fbmDIV { bottom:-1px !important; right:75px !important; min-width:170px; border:1px solid #b5b5b5; background:#e5e5e5; position:fixed !important; z-index:99999 !important; padding:0px; }'+
'#fbmHEAD { color:black; font-size:11px; padding:5px 6px 5px 6px; height:14px; min-width: 112px; padding-left: 21px; background-image: url(http://static.ak.fbcdn.net/images/presence/buddy_list.gif); background-repeat: no-repeat; background-position: 4px 6px; white-space: nowrap;}'+
'#fbmHEAD:hover { cursor:pointer; }'+
'#fbmEXIT { color:#6D84B4; float:right; font-weight:bold; padding:5px 6px 5px 6px; vertical-align:top; }'+
'#fbmEXIT:hover { cursor:pointer; color:#3B5998; }'+
'#fbmBODY { background-color:white; border-bottom:1px solid #b5b5b5; }'+
'#fbmLINK { padding:4px 8px 4px 8px; }'+
'#fbmNUM { font-weight:bold; }'
);
if (!onshow) { GM_addStyle('#fbmBODY {display: none;}'); }
 
// Create new friends menu
var div = document.createElement('div');
div.id = 'fbmDIV';
div.innerHTML = '<div id="fbmBODY"><div id="fbmLINK">Loading...</div></div><div id="fbmEXIT" title="Close">x</div><div id="fbmHEAD">Online Friends (<span id="fbmNUM">0</span>)</div>';
document.body.appendChild(div);
 
// Listen for clicks on the x and make the list disappear and stop updating
$('fbmEXIT').addEventListener('click', function() {
$('fbmDIV').style.display='none';
}, false);
// Listen for clicks on the header and hide/show the list of friends
$('fbmHEAD').addEventListener('click', function() {
if (onshow) { $('fbmBODY').style.display='none'; onshow = false; }
else { $('fbmBODY').style.display='block'; onshow = true; }
GM_setValue('ShowHide',onshow);
}, false);
}
 
// Updates the online friend list on top of Facebook Chat
function updatePopUp (temp,num) {
if (temp == "") { temp = '<div id="fbmLINK">No online friends.</div>'; }
if ($('fbmNUM')) { $('fbmNUM').innerHTML = num; }
if ($('fbmBODY')) { $('fbmBODY').innerHTML = temp; }
}
 
function $(id) { return document.getElementById(id); }
function getUID(rawt) { return rawt.substring(0, rawt.indexOf('"')); }
function getUName(rawt) { return rawt.substring(rawt.indexOf('.jpg"></div>')+12, rawt.length); }
 
function process(response) {
var result = new Array();
var res = response;
var i = 0;
var indexd, indexu;
while(res.indexOf('profile.php?id=') > 0) {
res = res.substring(res.indexOf('profile.php?id=')+15, res.length);
indexd = res.indexOf('<div class="listCaption2');
indexu = res.indexOf('</u>');
if ((indexd == -1) || (indexu < indexd)) { result[i] = res.substring(0, indexu); }
else { result[i] = res.substring(0, indexd); }
i++;
}
return result;
}
 
// User Script Commands in Greasemonkey
function MenuCommands (){
GM_registerMenuCommand('FBOnline: Show/Hide PopUp Friends', function(){
if ((GM_getValue('ShowPopUp', true)) && (confirm('Hide online friends in place of Facebook Chat?'))) {
GM_setValue('ShowPopUp', false);
}
else if ((!GM_getValue('ShowPopUp', true)) && (confirm('Show online friends in place of Facebook Chat?'))) {
GM_setValue('ShowPopUp', true);
}
});
GM_registerMenuCommand('FBOnline: Show/Hide Time', function(){
if ((GM_getValue('ShowTime', true)) && (confirm('Hide Time next to friend?'))) {
GM_setValue('ShowTime', false);
}
else if ((!GM_getValue('ShowTime', true)) && (confirm('Show Time next to friend?'))) {
GM_setValue('ShowTime', true);
}
});
}

Revision as of 15:13, 9 June 2009

// ==UserScript== // @name Facebook Online Logger // @namespace FBOnline // @include http://*.facebook.com* // @exclude http://*.facebook.com/login.php* // @exclude http://en-gb.facebook.com/* // @description Log out of Facebook Chat and still see who is online. // @author http://userscripts.org/users/59449 // @version 3.3 // ==/UserScript==

// Modified version of // http://userscripts.org/scripts/show/19450 // http://userscripts.org/scripts/show/23499

// Ad remover window.addEventListener("load", function(e) { var elements = xpath("//div[contains(@class, 'ad_capsule')] | //div[contains(@class, 'social_ad')] | //div[@id='announce'] | //div[contains(@id, 'sponsor')] | //div[contains(@id, 'ssponsor')]"); if (elements.snapshotLength > 0) { for (var i = 0; i < elements.snapshotLength; i++) { var thisElement = elements.snapshotItem(i); thisElement.parentNode.removeChild(thisElement); } } }, false); function xpath (query) { return document.evaluate(query, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); }

init(); function init() { if (!document.body) { window.setTimeout(function(){init();}, 300); } if (window != window.top) return; // Don't run in a frame if ($('loginform')) return; // Don't run if not logged in MenuCommands(); if (GM_getValue('ShowPopUp', true)) { createChatMenu(); } facebookOnline(); }

function facebookOnline() { GM_xmlhttpRequest({ method:"GET", url:"http://iphone.facebook.com/friends.php?v=online", headers:{"User-Agent":"Mozilla/5.0","Accept":"text/xml"}, onload:function(response) { if((response.readyState == 4) && (response.status == 200)) parseResults(response.responseText); } });

window.setTimeout(facebookOnline, 60000); // Refresh every 60 seconds }

function parseResults(temp) { var rt = process(temp);

var uLINK = ""; var popText = "";

var today = new Date(); var h = today.getHours(); if (h<10) h = "0"+h; var m = today.getMinutes(); if (m<10) m = "0"+m; var dd = today.getDate(); var mm = today.getMonth()+1; var yy = today.getFullYear(); var t = dd+"/"+mm+"/"+yy+" "+h+":"+m;

var lastchecked = GM_getValue("lastupdated", t);

for (var i=0; i<rt.length;i+=1) { var uID = getUID(rt[i]); var uNAME = getUName(rt[i]);

var a = GM_getValue(uID,uNAME+","+t+","+t); var b = a.split(","); var c;

if (lastchecked == b[2]) { c = b[1].split(" "); GM_setValue(uID,uNAME+","+b[1]+","+t); //was online } else { c = t.split(" "); GM_setValue(uID,uNAME+","+t+","+t); //now online }

if (GM_getValue('ShowTime', true)) { uLINK = c[1] + ' <a href="http://www.facebook.com/profile.php?id='+uID+'">'+uNAME+'</a>'; } else { uLINK = '<a href="http://www.facebook.com/profile.php?id='+uID+'">'+uNAME+'</a>'; }

popText += '

'; // For popup menu - add hh:mm here

}

if (GM_getValue('ShowPopUp', true)) { updatePopUp(popText,rt.length); } // Popup menu in place of chat

GM_setValue("lastupdated", t); }

// Adds the online friends list on top of the Facebook Chat bar function createChatMenu (){ var onshow = GM_getValue('ShowHide', false); if (onshow != true && onshow != false) { onshow = false; GM_setValue('ShowHide', onshow); }

GM_addStyle( '#fbmDIV { bottom:-1px !important; right:75px !important; min-width:170px; border:1px solid #b5b5b5; background:#e5e5e5; position:fixed !important; z-index:99999 !important; padding:0px; }'+ '#fbmHEAD { color:black; font-size:11px; padding:5px 6px 5px 6px; height:14px; min-width: 112px; padding-left: 21px; background-image: url(http://static.ak.fbcdn.net/images/presence/buddy_list.gif); background-repeat: no-repeat; background-position: 4px 6px; white-space: nowrap;}'+ '#fbmHEAD:hover { cursor:pointer; }'+ '#fbmEXIT { color:#6D84B4; float:right; font-weight:bold; padding:5px 6px 5px 6px; vertical-align:top; }'+ '#fbmEXIT:hover { cursor:pointer; color:#3B5998; }'+ '#fbmBODY { background-color:white; border-bottom:1px solid #b5b5b5; }'+ '#fbmLINK { padding:4px 8px 4px 8px; }'+ '#fbmNUM { font-weight:bold; }' ); if (!onshow) { GM_addStyle('#fbmBODY {display: none;}'); }

// Create new friends menu var div = document.createElement('div'); div.id = 'fbmDIV';

div.innerHTML = '

x
Online Friends (0)

';

document.body.appendChild(div);

// Listen for clicks on the x and make the list disappear and stop updating $('fbmEXIT').addEventListener('click', function() { $('fbmDIV').style.display='none'; }, false); // Listen for clicks on the header and hide/show the list of friends $('fbmHEAD').addEventListener('click', function() { if (onshow) { $('fbmBODY').style.display='none'; onshow = false; } else { $('fbmBODY').style.display='block'; onshow = true; } GM_setValue('ShowHide',onshow); }, false); }

// Updates the online friend list on top of Facebook Chat function updatePopUp (temp,num) {

if (temp == "") { temp = '

'; }

if ($('fbmNUM')) { $('fbmNUM').innerHTML = num; } if ($('fbmBODY')) { $('fbmBODY').innerHTML = temp; } }

function $(id) { return document.getElementById(id); } function getUID(rawt) { return rawt.substring(0, rawt.indexOf('"')); }

function getUName(rawt) { return rawt.substring(rawt.indexOf('.jpg">')+12, rawt.length); }

function process(response) { var result = new Array(); var res = response; var i = 0; var indexd, indexu;

while(res.indexOf('profile.php?id=') > 0) { res = res.substring(res.indexOf('profile.php?id=')+15, res.length); indexd = res.indexOf('<div class="listCaption2'); indexu = res.indexOf(''); if ((indexd == -1) || (indexu < indexd)) { result[i] = res.substring(0, indexu); } else { result[i] = res.substring(0, indexd); } i++; }

return result; }

// User Script Commands in Greasemonkey function MenuCommands (){ GM_registerMenuCommand('FBOnline: Show/Hide PopUp Friends', function(){ if ((GM_getValue('ShowPopUp', true)) && (confirm('Hide online friends in place of Facebook Chat?'))) { GM_setValue('ShowPopUp', false); } else if ((!GM_getValue('ShowPopUp', true)) && (confirm('Show online friends in place of Facebook Chat?'))) { GM_setValue('ShowPopUp', true); } }); GM_registerMenuCommand('FBOnline: Show/Hide Time', function(){ if ((GM_getValue('ShowTime', true)) && (confirm('Hide Time next to friend?'))) { GM_setValue('ShowTime', false); } else if ((!GM_getValue('ShowTime', true)) && (confirm('Show Time next to friend?'))) { GM_setValue('ShowTime', true); } }); }