I'm trying to write a bookmarklet that works on two different pages. I can successfully iterate through and process the elements once I get hold of them, but in order to accommodate the two different pages, I wanted to compile a list of DIVs by two different class names. I started with this:
traces=
[].concat.apply(document.getElementsByClassName('fullStacktrace'),
document.getElementsByClassName('msg'))
but on the first page it results this:
> traces=[].concat.apply(document.getElementsByClassName('fullStacktrace'),
document.getElementsByClassName('msg'))
[HTMLCollection[2]]
> traces[0]
[div.fullStacktrace, div.fullStacktrace]
> traces[1]
undefined
> traces[0][0]
<div class="fullStacktrace" style="height: auto;">…</div>
whereas on the other page
> traces=[].concat.apply(document.getElementsByClassName('fullStacktrace'),
document.getElementsByClassName('msg'))
[HTMLCollection[0], div#msg_2.msg.l1, ... div#msg_6460.msg.l1]
> traces[0]
[]
> traces[1]
<div class="msg l1" id="msg_2">…</div>
So getElementsByClassName works for both pages, but it looks like concat.apply doesn't iterate its first argument, but does iterate its second argument?!
So I tried using concat twice and going all out with parentheses:
traces=(
(
[].concat.apply(document.getElementsByClassName('fullStacktrace'))
)
.concat.apply(document.getElementsByClassName('msg'))
)
but it gets even stranger: the first page said:
Array[1]
> 0: HTMLCollection[0]
length: 0
> __proto__: HTMLCollection
length: 1
> __proto__: Array[0]
and the other:
Array[1]
> 0: HTMLCollection[283] // Lots of div#msg_3.msg.l1 sort of things in here
length: 1
>__proto__: Array[0]
get its full complement of divs.
Since the two groups of elements are only on one or the other page, I can just use a conditional in my particular case, but the behaviour above is very surprising to me, so I would like to understand it. Any ideas?
For reference, this is on Mac Chrome Version 56.0.2924.87 (64-bit)