M
Matej
Hi,
I have this greasemonkey function:
(function() {
var elementFound = Object();
var tempElem = Object();
var tempText = String("");
var contentString = String("");
var anchorTags = document.getElementsByTagName("a");
for (var i = 0; i < anchorTags.length ; i++)
{
elementFound = anchorTags;
contentString = elementFound.textContent.replace(/^\s*(.*)\s*$/,"$1");
//alert(contentString);
if (contentString == "<< Previous") {
elementFound.setAttribute("accesskey","P");
tempText = document.createTextNode("<< ");
elementFound.replaceChild(tempText,elementFound.firstChild);
tempElem = document.createElement("b");
tempText = document.createTextNode("P");
tempElem.appendChild(tempText);
elementFound.appendChild(tempElem);
tempText = document.createTextNode("revious");
elementFound.appendChild(tempText);
} else if (contentString == "Next >>") {
elementFound.setAttribute("accesskey","N");
tempElem = document.createElement("b");
tempText = document.createTextNode("N");
tempElem.appendChild(tempText);
elementFound.replaceChild(tempElem,elementFound.firstChild);
tempText = document.createTextNode("ext >>");
elementFound.appendChild(tempText);
} else if (contentString == "[Gallery Index]") {
elementFound.setAttribute("accesskey","I");
tempText = document.createTextNode("[Gallery ");
elementFound.replaceChild(tempText,elementFound.firstChild);
tempElem = document.createElement("b");
tempText = document.createTextNode("I");
tempElem.appendChild(tempText);
elementFound.appendChild(tempElem);
tempText = document.createTextNode("ndex]");
elementFound.appendChild(tempText);
};
}
})();
Now, this works, but it is ugly as hell. What I would like to do is to
use function fixElement, which would look something like this:
function fixElement(rootElement,beforeText,accKey,afterText) {
var tempElem = Object();
var tempText = String("");
rootElement.setAttribute("accesskey",accKey);
tempText = document.createTextNode(beforeText);
// I know, I should test on empty beforeText parameter, but
// for the sake of simplicity, let's keep it as it is.
rootElement.replaceChild(tempText,rootElement.firstChild);
tempElem = document.createElement("b");
tempText = document.createTextNode(accKey);
tempElem.appendChild(tempText);
rootElement.appendChild(tempElem);
tempText = document.createTextNode(afterText);
rootElement.appendChild(tempText);
return(rootElement);
}
Now, the problem is how to call this function. Can I do something like
this?
if (contentString == "<< Previous") {
tmpAnchorElement = fixElement(elementFound,"<< ","P","revious");
elementFound = tmpAnchorElement;
}
Somehow it seems not a good idea. However, I do not know how to get to
the parent element (so that I could use replaceChild). Any ideas?
Thanks,
Matej
I have this greasemonkey function:
(function() {
var elementFound = Object();
var tempElem = Object();
var tempText = String("");
var contentString = String("");
var anchorTags = document.getElementsByTagName("a");
for (var i = 0; i < anchorTags.length ; i++)
{
elementFound = anchorTags;
contentString = elementFound.textContent.replace(/^\s*(.*)\s*$/,"$1");
//alert(contentString);
if (contentString == "<< Previous") {
elementFound.setAttribute("accesskey","P");
tempText = document.createTextNode("<< ");
elementFound.replaceChild(tempText,elementFound.firstChild);
tempElem = document.createElement("b");
tempText = document.createTextNode("P");
tempElem.appendChild(tempText);
elementFound.appendChild(tempElem);
tempText = document.createTextNode("revious");
elementFound.appendChild(tempText);
} else if (contentString == "Next >>") {
elementFound.setAttribute("accesskey","N");
tempElem = document.createElement("b");
tempText = document.createTextNode("N");
tempElem.appendChild(tempText);
elementFound.replaceChild(tempElem,elementFound.firstChild);
tempText = document.createTextNode("ext >>");
elementFound.appendChild(tempText);
} else if (contentString == "[Gallery Index]") {
elementFound.setAttribute("accesskey","I");
tempText = document.createTextNode("[Gallery ");
elementFound.replaceChild(tempText,elementFound.firstChild);
tempElem = document.createElement("b");
tempText = document.createTextNode("I");
tempElem.appendChild(tempText);
elementFound.appendChild(tempElem);
tempText = document.createTextNode("ndex]");
elementFound.appendChild(tempText);
};
}
})();
Now, this works, but it is ugly as hell. What I would like to do is to
use function fixElement, which would look something like this:
function fixElement(rootElement,beforeText,accKey,afterText) {
var tempElem = Object();
var tempText = String("");
rootElement.setAttribute("accesskey",accKey);
tempText = document.createTextNode(beforeText);
// I know, I should test on empty beforeText parameter, but
// for the sake of simplicity, let's keep it as it is.
rootElement.replaceChild(tempText,rootElement.firstChild);
tempElem = document.createElement("b");
tempText = document.createTextNode(accKey);
tempElem.appendChild(tempText);
rootElement.appendChild(tempElem);
tempText = document.createTextNode(afterText);
rootElement.appendChild(tempText);
return(rootElement);
}
Now, the problem is how to call this function. Can I do something like
this?
if (contentString == "<< Previous") {
tmpAnchorElement = fixElement(elementFound,"<< ","P","revious");
elementFound = tmpAnchorElement;
}
Somehow it seems not a good idea. However, I do not know how to get to
the parent element (so that I could use replaceChild). Any ideas?
Thanks,
Matej