Skip to content

Instantly share code, notes, and snippets.

@gialloporpora
Created March 5, 2009 11:22
Show Gist options
  • Save gialloporpora/74306 to your computer and use it in GitHub Desktop.
Save gialloporpora/74306 to your computer and use it in GitHub Desktop.
Draft of unread command for MozillaItalia forum
function NounSMFAjax(name, url) {
var noun_ajax_prototype={
_name: name,
_url : url,
_topic_list: null,
_callback: function( unread) {
noun_ajax_prototype._topic_list=unread;
},
getData: function( callback) {
jQuery.get(noun_ajax_prototype._url, function ( data ) {
/* purtroppo il template di SMF è poco accessibile e bisogna fare una regex brutale per cercare i link */
var regex=/topicseen">([^<]*)<\/a> <a class="linkpulsanti" href="([^"]*)/g;
var unread=[];
while ((m = regex.exec(data))!=null){
unread.push({title: m[1], link : m[2]});
}
if (unread.length==0) {
if (data.search("<td>Attenzione!\/td>") != -1 ) callback(unread.push({"title" : "Eseguire login", "link" : "http://forum.mozillaitalia.org/index.php?action=login"}));
else callback(unread.push({"title" : "Vai al forum", "link" : "http://forum.mozillaitalia.org/index.php?action=unreadreplies"}));
}
callback(unread);
}); /* Fine chiamata Ajax con Jquery.get */
},
reset: function() {
noun_ajax_prototype._topic_list=null;
},
suggest: function(filter){
with ( noun_ajax_prototype ) {
if (! _topic_list) { /* if topic_list is not null */
_topic_list=[];
getData( _callback );
return _topic_list;
}
if ( _topic_list.length==0) return [];
var suggs=_topic_list;
if (filter.length>0) var suggestions=[];
for (i in suggs)
if (suggs[i].title.search(filter) != -1) suggestions.push({title : suggs[i].title, link : suggs[i].link});
return jQuery.map(suggestions,function(suggestion){
return CmdUtils.makeSugg(suggestion.title, null, suggestion.link, null);
});
}
}
}
return noun_ajax_prototype;
}
var noun_smf_mi_unread=NounSMFAjax("noun_smf_mi_unread", "http://forum.mozillaitalia.org/index.php?action=unread");
var noun_smf_mi_unreadreplies=NounSMFAjax("noun_smf_mi_unreadreplies", "http://forum.mozillaitalia.org/index.php?action=unreadreplies");
CmdUtils.CreateCommand({
name: "mi-unread",
takes:{input:noun_smf_mi_unread},
preview: function( pblock, input) {
s="";
for (i in input){
s+= i + " -> " + input[i]+"<br>";
}
if (input.text=="none " ) {
s+="on ci sono messaggi oppure non sei loggato al forum";
noun_smf_mi_unread.reset();
}
pblock.innerHTML=s;
},
execute: function ( input ){
doc=CmdUtils.getDocument().location.replace(input.data);
noun_smf_mi_unread.reset();
}
});
CmdUtils.CreateCommand({
name: "mi-unreadreplies",
takes:{input:noun_smf_mi_unreadreplies},
preview: function( pblock, input) {
s="";
for (i in input){
s+= i + " -> " + input[i]+"<br>";
}
if (input.text=="none " ) {
s+="on ci sono messaggi oppure non sei loggato al forum";
noun_smf_mi_unreadreplies.reset();
}
pblock.innerHTML=s;
},
execute: function ( input ){
doc=CmdUtils.getDocument().location.replace(input.data);
noun_smf_mi_unreadreplies.reset();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment