How could I get the innerHTML of a clicked element on a page of a random website, using a favelet or a script injector?
By example, clicking right above the "Ask Question" of this page would return something starting with: <div class="nav mainnavs " style="">
<ul style="">
<li style="">
.
I tried editing a favelet from slayeroffice.com whose initial purpose was removing children of the selected element, but it doesn't seems to work.
javascript:
var b = new Array();
var c= 1;
var d;
document.onkeydown = ck;
el = document.getElementsByTagName('*');
for (i = 0; i < el.length; i++)
{
if (el[i].tagName.search(/(HTML|BODY)/i) == -1)
{
if (el[i].title) el[i].oldTitle = el[i].title;
el[i].title = el[i].innerHTML;
d=String(el[i].innerHTML);
el[i].onclick = function(e)
{
t = this;
if (window.event) e = window.event;
if ((t == e.target) || (window.event)) alert(d);
if (window.opera) e.stopPropagation();
return false;
};
el[i].onmouseover = function()
{
if (!c) return;
c = 0;
t = this;
b[t] = t.style.backgroundColor;
t.style.background = '#DADADA';
};
void(
el[i].onmouseout = function()
{
t = this;
t.style.backgroundColor = b[t];
c = 1;
});
}
}
function ck(e)
{
k = window.event ? window.event.keyCode : e.keyCode;
if (k == 27)
{
for (i = 0; i < el.length; i++) {
if (el[i].tagName.search(/(HTML|BODY)/i) == -1)
{
el[i].oldTitle ? el[i].title = el[i].oldTitle : el[i].removeAttribute('title');
el[i].onclick = null;
el[i].onmouseover = null;
el[i].onmouseout = null;
el[i].style.backgroundColor = b[t];
}
}
}
}
Is there any solution?
You can just attach an event to the document onclick handler. No need for looping through every element on the page. Clicks will bubble up to it...
document.onclick = function(event) {
var target = event.target || event.srcElement;
alert ( target.innerHTML );
};
However I recommend using a better method for attaching the event handler than with '=' as it will override any previously defined events that may be crucial to page operation.
I have used this snippet before for better event attaching without jQuery...
var bindEvent = function(elem ,evt,cb) {
//see if the addEventListener function exists on the element
if ( elem.addEventListener ) {
elem.addEventListener(evt,cb,false);
//if addEventListener is not present, see if this is an IE browser
} else if ( elem.attachEvent ) {
//prefix the event type with "on"
elem.attachEvent('on' + evt, function(){
/* use call to simulate addEventListener
* This will make sure the callback gets the element for "this"
* and will ensure the function's first argument is the event object
*/
cb.call(event.srcElement,event);
});
}
}
You just call...
bindEvent(document,'click', function(event) {
var target = event.target || event.srcElement;
alert ( target.innerHTML );
});
You will have issues with the scope of i
in a for loop.
i
will be equal to el.length
when the clicking happens.
But why use a variable, when you can read it
change
if ((t == e.target) || (window.event)) alert(d);
to
if ((t == e.target) || (window.event)) {
alert(this.innerHTML);
}
Also shouldn't you be checking for srcElement on window.event?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With