Last active
December 17, 2015 09:58
-
-
Save shimondoodkin/5590864 to your computer and use it in GitHub Desktop.
in chrome, add Tampermonkey plugin.
add new script.
copy paste to new script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name todo list in facebook | |
// @namespace http://kodtov.com | |
// @version 0.1 | |
// @description adds a todo list in top of facebook | |
// @include *facebook.com* | |
// @copyright 2013, Shimon Doodkin | |
// ==/UserScript== | |
// tinyxhr by Shimon Doodkin - licanse: public doamin - https://gist.github.com/4706967 | |
// | |
// get example: tinyxhr("http://site.com/ajaxaction",function (err,data,xhr){ if (err) console.log("goterr ",err,'status='+xhr.status); console.log(data) }); | |
// post example: tinyxhr("http://site.com/ajaxaction",function (err,data,xhr){ if (err) console.log("goterr ",err,'status='+xhr.status); console.log(data) },'POST','value1=1&value2=2'); | |
// post example with mime: tinyxhr("http://site.com/ajaxaction.json",function (err,data,xhr){ if (err) console.log("goterr ",err,'status='+xhr.status); console.log(data); console.log(JSON.parse(data)) },'POST',JSON.stringify({value:1}),'application/javascript'); | |
// cb - a callback function like: function (err,data,XMLHttpRequestObject){ if (err) throw err; } | |
// | |
function tinyxhr(url,cb,method,post,contenttype) | |
{ | |
var requestTimeout,xhr; | |
try{ xhr = new XMLHttpRequest(); }catch(e){ | |
try{ xhr = new ActiveXObject("Msxml2.XMLHTTP"); }catch (e){ | |
if(console)console.log("tinyxhr: XMLHttpRequest not supported"); | |
return null; | |
} | |
} | |
requestTimeout = setTimeout(function() {xhr.abort(); cb(new Error("tinyxhr: aborted by a timeout"), "",xhr); } | |
, 10000); | |
xhr.onreadystatechange = function() | |
{ | |
if (xhr.readyState != 4) return; | |
clearTimeout(requestTimeout); | |
cb(xhr.status != 200?new Error("tinyxhr: server respnse status is "+xhr.status):false, xhr.responseText,xhr); | |
} | |
xhr.open(method?method.toUpperCase():"GET", url, true); | |
//xhr.withCredentials = true; | |
if(!post) | |
xhr.send(); | |
else | |
{ | |
xhr.setRequestHeader('Content-type', contenttype?contenttype:'application/x-www-form-urlencoded'); | |
xhr.send(post) | |
} | |
} | |
//tinyxhr("/test",function (err,data,xhr){ if (err) console.log("goterr ",err); console.log(data) }); | |
var lasttododata=null; | |
function savetodo(force,data) | |
{ | |
lasttododata=data; | |
var user_id = document.cookie.match(document.cookie.match(/c_user=(\d+)/)[1])[0]; | |
if(force) | |
{ | |
tinyxhr( 'https://api.mongolab.com/api/1/databases/facebooktodo/collections/todo?apiKey=a_zBx6UrgSoOqSXG0QQVuoG8WZNxvJO8&u=true&q='+encodeURIComponent('{"_id":'+user_id+'}'), | |
function (err,data,xhr){ if (err) {console.log("goterr ",err); } console.log("upsert",data); | |
document.getElementById('todolistsaved').innerHTML=err||'saved!'; | |
if(!err) setTimeout(function(){document.getElementById('todolistsaved').innerHTML=''},2500); | |
}, | |
"PUT", JSON.stringify( { "$set" : { "todo" : data } } ),"application/json"); | |
} | |
else | |
{ | |
if(window.savetodo_timeout) | |
{ | |
clearTimeout(window.savetodo_timeout); | |
} | |
window.savetodo_timeout=setTimeout(function() | |
{ | |
console.log('todo saved'); | |
tinyxhr( 'https://api.mongolab.com/api/1/databases/facebooktodo/collections/todo?apiKey=a_zBx6UrgSoOqSXG0QQVuoG8WZNxvJO8&u=true&q='+encodeURIComponent('{"_id":'+user_id+'}'), | |
function (err,data,xhr){ if (err) {console.log("goterr ",err);} | |
//console.log("upsert",data) ; | |
document.getElementById('todolistsaved').innerHTML=err||'saved!'; | |
if(!err) setTimeout(function(){document.getElementById('todolistsaved').innerHTML=''},2500); | |
}, | |
"PUT",JSON.stringify( { "$set" : { "todo" : data } } ),"application/json"); | |
},2000); | |
} | |
} | |
function loadtodo(cb) | |
{ | |
if(lasttododata)return cb(lasttododata); | |
var user_id = document.cookie.match(document.cookie.match(/c_user=(\d+)/)[1])[0]; | |
tinyxhr( 'https://api.mongolab.com/api/1/databases/facebooktodo/collections/todo/'+user_id+'?apiKey=a_zBx6UrgSoOqSXG0QQVuoG8WZNxvJO8', | |
function (err,data,xhr){ | |
if (err) {console.log("goterr ",err);} | |
// console.log("select",data) ; | |
cb(data); | |
} | |
); | |
} | |
var removedtimeout | |
function addtodolist() | |
{ | |
if(document.getElementById('pagelet_group_composer')!=null||document.getElementById('pagelet_composer')!=null||document.querySelector(".fbTimelineComposerCapsule")!=null) | |
{ | |
if(removedtimeout){ clearTimeout(removedtimeout);} | |
if(document.getElementById('todolisttext')) return; | |
/* | |
if(localStorage.todolist_text) | |
{ | |
document.getElementById('todolisttext').value=localStorage.todolist_text; | |
} | |
*/ | |
loadtodo(function(text){ | |
var t = document.createElement("div"); | |
t.id="todolist"; | |
t.innerHTML='Todo: <span id="todolistsaved"></span><br><Textarea dir="rtl" id="todolisttext" style="width:100%;height:150px">1. \n2. \n3. \n</Textarea>'; | |
if(document.querySelector(".fbTimelineComposerCapsule")) | |
var parent_el=document.querySelector(".fbTimelineComposerCapsule").parentNode.parentNode; | |
else if(document.getElementById('contentArea')) | |
var parent_el=document.getElementById('contentArea'); | |
parent_el.insertBefore(t,parent_el.firstChild); | |
t.addEventListener("DOMNodeRemovedFromDocument", function (ev) { | |
//if(document.getElementById('todolisttext')) return; | |
if(removedtimeout)clearTimeout(removedtimeout); | |
removedtimeout=setTimeout(addtodolist,1000); | |
}, false); | |
document.getElementById('todolisttext').onchange=function(){savetodo(true,this.value)}; | |
document.getElementById('todolisttext').onkeyup=function(){savetodo(false,this.value)}; | |
document.getElementById('todolisttext').value=JSON.parse(text).todo||"1. \n2. \n3. \n"; } | |
); | |
} | |
else | |
removedtimeout=setTimeout(addtodolist,1000); | |
} | |
addtodolist() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment