InnerHTML Clicked Element

How can I get the innerHTML of a clicked element on a random website page using a falet or injector script? For example, right-clicking on “Ask a Question” on this page will return something, starting with: <div class="nav mainnavs " style=""> <ul style=""> <li style=""> .

I tried to edit the favelet from slayeroffice.com, whose original goal was to remove the children of the selected item, but didn't seem 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?

+4
source share
2 answers

You can simply attach the event to the onclick handler of the document. There is no need to iterate over each element on the page. Clicks will bubble up to him ...

 document.onclick = function(event) { var target = event.target || event.srcElement; alert ( target.innerHTML ); }; 

However, I recommend using a better method for attaching an event handler than with '=', as it will override any previously defined events that may be critical to the operation of the page.

I used this snippet before to better add events 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 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 ); }); 
+7
source

You will have problems with volume i in the for loop.

i will be equal to el.length when the click occurs.

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 you should not check srcElement on window.event?

+2
source

Source: https://habr.com/ru/post/1481395/


All Articles