Συζητήσεις » Ανάπτυξη

GM_xmlhttpRequest response changes after "onload" event??

§
Δημοσιεύτηκε: 02/10/2014
Επεξεργάστηκε: 06/10/2014

GM_xmlhttpRequest response changes after "onload" event??

I'm pulling data from a site using GM_xmlhttpRequest. On "onload" an anonymous function copies the response text into a new node as innerHTML, loops through all nodes and adds a class to these. After this, the result gets appended to the main document.

The funny thing here is that not all nodes gets the new class added. Can someone explain?

function getData() {
	GM_xmlhttpRequest({
		method: "GET",
		url: "http://somewebsite.com",
		onload: function(response) {
			var el = document.createElement('div');
			el.innerHTML = response.responseText;
			applyClass(el);
			var txt = el.getElementsByClassName("head-big")[0].innerHTML;
			txt += el.getElementsByClassName("def-list")[0].innerHTML;
			document.getElementById('mySpan').innerHTML = txt;
		}
	});
}
function applyClass(el) {
	var c=0;
	var elems = el.querySelectorAll("*");
	for (var i=0; i < elems.length; i++) {
		if (elems[i].tagName != "SCRIPT" && elems[i].tagName != "TITLE" && elems[i].tagName != "LINK" && elems[i].tagName != "STYLE" && elems[i].tagName != "IFRAME") {
			if (elems[i].className == "") {
				elems[i].className = "myClass";
				c+=1;
			}else{
				elems[i].className += " myClass";
				c+=1;
			}
		}
	}
}
woxxomΣυντονιστής
§
Δημοσιεύτηκε: 02/10/2014
Επεξεργάστηκε: 06/10/2014

Without a sample of the html the most obvious&dumb answer would be that the excluded nodes are SCRIPT, TITLE, LINK, STYLE, IFRAME because of the IF clause you have written. I'd recommend using a more obvious indenting.

§
Δημοσιεύτηκε: 06/10/2014

@wOxxOm: Indentation was lost despite the use of the HTML CODE tag. I've fixed that for you now using the PRE tag.

Excluded nodes by the IF statement obviously does not get altered. I thought that went without saying. For the sake of clarity, I could remove that IF statement and still encounter the same issue.

woxxomΣυντονιστής
§
Δημοσιεύτηκε: 06/10/2014
Επεξεργάστηκε: 06/10/2014

Okay, is it random or every time the same nodes are skipped? If it's the latter, post the html after applyClass() here, please. Also, what happens in other browsers (FF/Chrome/IE)?

Δημοσίευση απάντησης

Συνδεθείτε για να δημοσιεύσετε μια απάντηση.