Greasy Fork is available in English.

게시판 » 개발

GM_xmlhttpRequest response changes after "onload" event??

§
작성: 2014-10-02
수정: 2014-10-06

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관리자
§
작성: 2014-10-02
수정: 2014-10-06

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.

§
작성: 2014-10-06

@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관리자
§
작성: 2014-10-06
수정: 2014-10-06

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)?

댓글 남기기

댓글을 남기려면 로그인하세요.