1

Is it anyhow possible to determine whether an DOM Element was originally in the sourcecode or injected through another js e.g. an adserver?

Watching mutations is not an solution since I'm getting access to the page after the page is rendered.

Manuel
  • 9,112
  • 13
  • 70
  • 110
  • 3
    I bet there's no way to do that, if you only start observing a completely rendered DOM. The DOM itself doesn't distinguish its nodes by whether one was rendered from HTML or through DOM manipulations. – rishat Mar 08 '16 at 12:22
  • 2
    The only way I can think of would be to reload the page in another window or in an iframe, and add the MutationObserver on this one. Then you can get the position in DOM and find back which elements do correspond in the original window. – Kaiido Mar 08 '16 at 12:40
  • @Kaiido, listening mutations can I get the source of mutation? So can I see what triggered the mutation? – Manuel Mar 08 '16 at 13:45
  • @RishatMuhametshin unluckily it really appears to be like that. This is too sad. More and more external scripts are included into pages and noone knows/cares what they do :/ have you never wonderded which script manipulates what? – Manuel Mar 08 '16 at 13:49
  • @Manuel, no I don't think you can. I'm not a MO specialist and I may be wrong but in my memory you only get the event of the mutation, the target of the event (the one mutated and the appended one in case of a node insertion) and the type of the mutation. Like any event, I don't think there is any info about what part of the code triggered it. – Kaiido Mar 08 '16 at 14:26

3 Answers3

2

You could send an XMLHttpRequest to the same URL to GET the original site contents, parse the DOM, remove any scripts you don't like, put it into an offscreen iframe so the non-external scripts can run and compare the contents of that iframe to current contents.

I made two fiddles to show how this could be done: target site, your JS

Comparison of the documents could be done with isEqualNode

Community
  • 1
  • 1
clinei
  • 238
  • 1
  • 7
  • You should at least compare the result of a `DOMParser` parsing. But setting an attribute on an element through js doesn't mean that you have injected the element. – Kaiido Mar 08 '16 at 12:35
  • Is there any alternative to DOMParser which executes inline js but does not include external js? – Manuel Mar 08 '16 at 13:47
  • @Manuel `DOMParser` doesn't execute any scripts. You could remove scripts from the parsed DOM based on their `src` attribute and then put the contents in an offscreen `iframe`, let the contents load and then compare __that__ – clinei Mar 08 '16 at 14:12
1

if you what to check the source html you can make a ajax request to the page, get the content and check for differences

Zamboney
  • 2,002
  • 12
  • 22
0

If you can save HTML source before any further js actions, than compare new HTML to old one and if they are not equal - element inserted via js.

Justinas
  • 41,402
  • 5
  • 66
  • 96
  • 1
    "Watching mutations is not an solution since I'm getting access to the page after the page is rendered." – clinei Mar 08 '16 at 12:15
  • Even if there wasn't this restriction, `document.body.style.width = '500px'` will change the outerHTML even if you didn't injected the body element through js. – Kaiido Mar 08 '16 at 12:37