// ==UserScript==
// @name memes
// @namespace No page
// @description Add Memes to SMF
// @author brunoais modify by Al_eXs
// @include *?action=post;topic=*
// @include *?action=post;board=*
// @include *?action=post;msg=*
// @include *?action=post;quote=*
// @include *?action=post;last_msg*
// @include *post&do=reply_post*
// @include *post&do=new_pos*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_registerMenuCommand
// @version 1.1.5
// ==/UserScript==
var smiliesUrl = GM_getValue("smilies", "|").split("|");
smiliesUrl.pop();
//////////////// ADD YOUR SMILIES HERE!!!!!!!! ///////////////////
//smiliesUrl.push("http://img684.imageshack.us/img684/8940/37380222.gif");
/* To add a smile You just need to copy this code:
smiliesUrl.push("");
and insert the url for the image between the "", like this:
smiliesUrl.push("http://i23.photobucket.com/albums/b371/K0modoDragon/Smileys/nosebleed.gif");
*/
//////////////// UNLESS YOU WANT TO CHANGE THE CODE, DON'T MESS UP WITH WHAT'S BELOW!!!!!!!! ///////////////////
/* Replace a text in the textarea
* @author bakabt
*/
function putInTxtarea(text, textarea) {
// Mozilla text range replace.
if (typeof(textarea.selectionStart) != "undefined") {
var begin = textarea.value.substr(0, textarea.selectionStart);
var end = textarea.value.substr(textarea.selectionEnd);
var scrollPos = textarea.scrollTop;
textarea.value = begin + text + end;
if (textarea.setSelectionRange)
{
textarea.focus();
textarea.setSelectionRange(begin.length + text.length, begin.length + text.length);
}
textarea.scrollTop = scrollPos;
}
// Just put it on the end.
else {
textarea.value += text;
textarea.focus(textarea.value.length - 1);
}
}
function xpath(query, object) {
if(!object) var object = document;
return document.evaluate(query, object, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
}
//Try the path for SMF 1.*
function getXpathRes1(){
var path = xpath("//tr/td/textarea[@name='message']");
return (path.snapshotLength > 0) ? path : false;
}
//Try the path for SMF 2.*
function getXpathRes2(){
var path = xpath("//div/div/textarea[@name='message']");
return (path.snapshotLength > 0) ? path : false;
}
//Try the path for IP Board*
function getXpathRes3(){
var path = xpath("//div/div/textarea[@name='Post']");
return (path.snapshotLength > 0) ? path : false;
}
//post
var ipbrd = getXpathRes3();
function insertImgsIntoElement2 (elem, urls){
for(var i = 0; i < urls.length; i++) {
var img = document.createElement("img");
img.src = urls[i];
img.style.cursor = "pointer";
var listner = function(url2){
return function(){
putInTxtarea(' [img]' + url2 + '[/img]',
document.getElementsByName('Post')[0]);
};
}(urls[i]);
img.addEventListener('click', listner, false);
elem.appendChild(img);
}
}
function insertImgsIntoElement (elem, urls){
for(var i = 0; i < urls.length; i++) {
var img = document.createElement("img");
img.src = urls[i];
img.style.cursor = "pointer";
var listner = function(url2){
return function(){
putInTxtarea(' [img]' + url2 + '[/img]',
document.getElementsByName('message')[0]);
};
}(urls[i]);
img.addEventListener('click', listner, false);
elem.appendChild(img);
}
}
function MostrarDiv(){
if (elem.id == "mySmilies"){
elem.style.display = (elem.style.display == 'none') ? 'block' : 'none';
}
}
var xpathRes = getXpathRes1();
var xpathRes2 = getXpathRes2();
// SMF 1.0.*
if(xpathRes){
var div1 = document.createElement("div");
div1.style="margin:0px; margin-top:5px; margin-bottom:5px";
div1.align ="center";
var btn = document.createElement("input");
btn.type="button";
btn.value="Mostrar/Ocultar Iconos";
btn.addEventListener("click", MostrarDiv, false);
div1.appendChild(btn);
var elem = document.createElement("div");
elem.id = "mySmilies";
elem.innerHTML = "";
elem.style.display = "none";
insertImgsIntoElement(elem, smiliesUrl);
div1.appendChild(elem);
xpathRes.snapshotItem(0).parentNode.insertBefore(div1, xpathRes.snapshotItem(0));
}else if(xpathRes2){
// SMF 2.0.*
var div1 = document.createElement("div");
div1.style="margin:-80px; margin-top:5px; margin-bottom:5px";
div1.align ="center";
var btn = document.createElement("input");
btn.type="button";
btn.value="Mostrar/Ocultar Iconos";
btn.addEventListener("click", MostrarDiv, false);
div1.appendChild(btn);
var elem = document.createElement("div");
elem.id = "mySmilies";
elem.innerHTML = "";
elem.style.display = "none";
insertImgsIntoElement(elem, smiliesUrl);
div1.appendChild(elem);
xpathRes2.snapshotItem(0).parentNode.parentNode.parentNode.insertBefore(div1, xpathRes2.snapshotItem(0).parentNode.parentNode);
}else{
// IP Board
var xpathRes = getXpathRes3();
if(xpathRes){
var div1 = document.createElement("div");
div1.style="margin:10px; margin-top:5px";
var btn = document.createElement("input");
btn.type="button";
btn.value="Mostrar/Ocultar Iconos";
btn.onclick="MostrarDiv()";
div1.appendChild(btn);
var elem = document.createElement("div");
elem.id = "mySmilies";
elem.innerHTML = "";
elem.style.display = "none";
insertImgsIntoElement2(elem, smiliesUrl);
div1.appendChild(elem);
xpathRes.snapshotItem(0).parentNode.parentNode.parentNode.insertBefore(div1, xpathRes.snapshotItem(0).parentNode.parentNode);
}
}
var menu=function() {
var img=prompt("Url for the smilie");
if (img!=null && img != "")
{
GM_setValue("smilies", (GM_getValue("smilies", "") + img + "|") );
alert('smilie added');
}
}
GM_registerMenuCommand("SMF:New smilie", menu);
var menu=function() {
var smilies = GM_getValue("smilies", "").split("|");
smilies.pop();
var promptOut = "";
for (num in smilies){
promptOut +=num + "->" + smilies[num] + "\n";
}
if(smilies.length == 0){
alert("There are no smilies");
return false;
}
var option = prompt("Which one do you want to edit/eliminate?(The number)\n"+promptOut, "");
if(option != null && !isNaN(option) && option < smilies.length ){
var newUrl = prompt("Change the url to edit it or erase it to delete it", smilies[option]);
if(newUrl != null && newUrl.length > 0){
smilies[option] = newUrl;
alert('smilie changed');
}else if(newUrl != null && newUrl.length == 0){
smilies.splice(option,1);
alert('smilie deleted');
}
GM_setValue("smilies",smilies.join("|")+"|" );
}
}
GM_registerMenuCommand("SMF:Edit/Erase Smilie", menu);