Help with smiley replacement?

in Script development
Subscribe to Help with smiley replacement? 11 posts, 4 voices



itsjareds Scriptwright

Hi, I am writing a script that replaces text smilies with images using Smilize. It worked right out-of-the-box, but I edited it some (mostly regex) and now it's not working.

Can anyone see what's wrong with this script?


function smilize() {
	try {
/*
	Regular Expressions to match all the smileys.
	// [\(\*\:;8][\w]*[dpo\)\(]?\*?(?![\w])(?![\/\/])
*/
		var smileyRegex = /((\*[\d\w]*\*)|[:;][\w()])/gi;
/*
	Regular Expression to match smiley if it is at the starting of line.
*/
		var smileyAtStartRegex = /^((((?:\*)[:;][\w()](?:\*)|[:;][\w()]))|(\*[\w]*\*))/i;
/*
	Regular Expressions to match individual smileys.
*/
		var smileysRegex = [ /:\)/ , /;\)/ , /:D/i , /:P/i , /:\(/ , /:o/i , /\*ill\*/i , /\*cool\*/i ,
 /\*yawn\*/i , /\*mad\*/i , /\*cry\*/i , /\*blush\*/i , /\*confused\*/i , /\*love\*/i ,
 /\*mean\*/i , /\*glad\*/i ];

/*
	DATA URIs for holding the smiley image data to be embedded into the page.
	Naming convention used is self explanatory.
	DATA URI generation courtesy - http://software.hixie.ch/utilities/cgi/data/data
*/
		var smileys = [ "data:image/gif;base64,R0lGODlhEYYOhETY0gCIT49 ... (large array of data:uris)" ];

/*
	XPath for getting all the text nodes in the html document containing the smileys.
*/
		var smileElements = document.evaluate("//text()[not(ancestor::script) "+
								"and not(ancestor::style) " + 
								"and not(ancestor::textarea) " +
								"and (contains(., ':)' ) " +
								"or contains(., ';)' ) " +
								"or contains(., ':D' ) " +
								"or contains(., ':d' ) " +
								"or contains(., ':P' ) " +
								"or contains(., ':p' ) " +
								"or contains(., ':(' ) " +
								"or contains(., ':o' ) " +
								"or contains(., ':O' ) " +
								"or contains(., '*cool*' ) " +
								"or contains(., '*yawn*' ) " +
								"or contains(., '*cry*' ) " +
								"or contains(., '*love*' ) " +
								"or contains(., '*confused*' ) " +
								"or contains(., '*mad*' ) " +
								"or contains(., '*ill*' ) " +
								"or contains(., '*glad*' ) " +
								"or contains(., '*blush*' ) " +
								"or contains(., '*mean*' ) ) ] ",
							document,
							null,
							XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
							null);

/*
	Beginning traversing through the element nodes that the XPath expression returned.
	I choose to go from last node to first node so as to avoid any discrepancies since
	I will be adding nodes to the document and editing it's structure.
*/
		for(var i = smileElements.snapshotLength - 1;i >= 0; i--) {	
			var elm = smileElements.snapshotItem(i);
			if(smileyRegex.test(elm.nodeValue)) {
				var elmSpan = document.createElement("span");
				elm.parentNode.replaceChild(elmSpan, elm);
				var text = elm.nodeValue;
/*
	Checking if smiley is at the starting of the line.
	If found replace it with appropriate smiley and then alter the 'text' of the element
	node cached to incorporate it having been replaced by an appropriate smiley.
*/
				if(smileyAtStartRegex.test(text)) {
					var smiley = smileyAtStartRegex.exec(text);
					text = text.substring(text.indexOf(smiley)+smiley.length+1);
					var smileyData=getSmiley(smileysRegex, smileys, smiley);
					var elmSmile = document.createElement("img");
					elmSmile.setAttribute("src", smileyData);
					elmSmile.setAttribute("align", "center");
					elmSmile.style.border = "none";
					elmSpan.appendChild(elmSmile);
				}
/*
	Setting the Smiley Regular Expression's lastIndex to 0 to start traversing through
	all the smileys that occur in the text node using 'exec' function.
*/
				smileyRegex.lastIndex = 0;
				for(var match = null, lastLastIndex = 0; (match = smileyRegex.exec(text)); ) {
					elmSpan.appendChild(document.createTextNode(text.substring(lastLastIndex, match.index)+RegExp.$1));
					var smiley = text.substring(match.index,smileyRegex.lastIndex);
					var smileyData=getSmiley(smileysRegex, smileys, smiley);
					var elmSmile = document.createElement("img");
					elmSmile.setAttribute("src", smileyData);
					elmSmile.setAttribute("align", "center");
					elmSmile.style.border = "none";
					elmSpan.appendChild(elmSmile);
					lastLastIndex = smileyRegex.lastIndex;
				}
				elmSpan.appendChild(document.createTextNode(text.substring(lastLastIndex)));
				elmSpan.normalize();
			}
/*
	Case when the smiley at the start is the only smiley in that particular node.
*/
			else if(smileyAtStartRegex.test(elm.nodeValue)) {
				GM_log(elm.nodeValue);
				var elmSpan = document.createElement("span");
				elm.parentNode.replaceChild(elmSpan, elm);
				var smiley = smileyAtStartRegex.exec(elm.nodeValue);
				var smileyData=getSmiley(smileysRegex, smileys, smiley);
				var elmSmile = document.createElement("img");
				elmSmile.setAttribute("src", smileyData);
				elmSmile.setAttribute("align", "center");
				elmSmile.style.border = "none";
				elmSpan.appendChild(elmSmile);
				elmSpan.appendChild(document.createTextNode(elm.nodeValue.substring(elm.nodeValue.indexOf(smiley)+smiley.length+1)));
				elmSpan.normalize();
			}
		}
	} catch (e) {
		GM_log(e);	//Catch and log any errors if encountered during the entire process.
	}
}

/*
	function to get the appropriate smiley from smileys array, by matching the smiley with
	each smileys regular expression.
*/
function getSmiley(smileysRegex, smileys, smiley) {
	for(var j=0;j<smileys>

 
itsjareds Scriptwright

please?

 
gollum Scriptwright

Be helpful to know which parts appear to not be working.

Have you done some GM_log(...)'s to find if your getting what you expect from your regex expressions?

What page are you testing this against?

 
znerp Scriptwright

It worked right out-of-the-box, but I edited it some (mostly regex) and now it's not working.
..so you're editing a functioning script to try to make it do exactly what it already does?

 
itsjareds Scriptwright

no, i changed a few of the smilies and their text emotes

 
itsjareds Scriptwright

Have you done some GM_log(...)'s to find if your getting what you expect from your regex expressions?

I did now that you told me to. It says that for every time it goes through the
for(var i = smileElements.snapshotlength - l; i >= 0; i--) part, it goes through the smileyRegex.lastIndex = 0; part twice.

What page are you testing this against?
Forum: Test: :P*:P* - Spogg.com

 
dob Scriptwright

Mikado used to have a script for that, but it seems offline.
Since there is no way of contacting ppl through PMs, I took the liberty of pasting it: http://o0t.de/nopaste/paste.php?f=jbjhvx

 
itsjareds Scriptwright

I mean what znerp did, with the green? Or is that not a quote?

 
gollum Scriptwright

<blockquote>I mean what znerp did, with the green? Or is that not a quote?</blockquote>

As well as Mikado's quote helper, there's this by alien_scum Userscripts comment helper

 
gollum Scriptwright

may be helpful?? regex online checkers
http://www.rubular.com/
http://gskinner.com/RegExr/
http://www.regextester.com/

 
itsjareds Scriptwright

I tested the regex using RegexPal, and the regular expressions are not the problem. And thanks for the blockquote help

Cross
Presentational HTML allowed.
Use <code> for inline code and <pre> for code blocks. Use &lt; and &gt; for literal < and >.
We help break paragraphs and link your links.
or cancel