Reputation: 6054
First of all I'm not quite sure this is the place for this kind of question, if not please tell me and I'll remove it.
I'm developing a script that modifies the DOM a bit and I'd like to test it in real sites to see if it behaves correctly and to detect issues.
I was wondering how simulate that the script is at that page and if it would be possible by using chrome dev-tools.
At first I tried adding the script to the but the script doesn't execute.
I tried writing this on the console but didn't work:
var script1 = document.createElement("script");
var script2 = document.createElement("script");
var head = document.querySelector("head");
var text = 'myscript();';
script2[(script2.innerText===undefined?"textContent":"innerText")] = text;
script1.setAttribute("src", "http://mysite.myscript.js");
head.appendChild(script1);
script1.onload = function(){ head.appendChild(script2) };
EDIT: Actually that script worked but in the inside I was listening to a DOMcontentLoad event, so of course when I executed that from the console, the dom was already loaded.
Upvotes: 0
Views: 108
Reputation: 10981
This can be done using the DevTools. To make it easy to do in many places, you should take advantage of a feature called Snippets.
This is the code I used to test in my snippet. I changed the "mysite" URL to pull a copy of Material Design Lite's JS and set the text to log the componentHandler object. Other than these changes, it is your code:
var script1 = document.createElement("script");
var script2 = document.createElement("script");
var head = document.querySelector("head");
var text = 'console.log(componentHandler);';
script2[(script2.innerText===undefined?"textContent":"innerText")] = text;
script1.setAttribute("src", "https://storage.googleapis.com/code.getmdl.io/1.0.0/material.min.js");
head.appendChild(script1);
script1.onload = function(){ head.appendChild(script2) };
With this you need to do the following steps:
To verify further that this is working, you can go check the Elements Panel.
Upvotes: 1