Place 'script' before the end of BODY tags with jQuery

google-chrome-extension, javascript, jquery

Solution

You need the content script to do the insert on every page you want.

The code of the content script is really simple and doesn't need jQuery.

var code = "your script code here";
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.appendChild(document.createTextNode(code));
document.body.appendChild(script);

As it will only be called once you don't even need to define a function. You can debug the code using debugger on any web the content script is attaching (F12) you will see your code in the content script tab.

Problem

This is the 'script' I want before the 'body' tag: ``` <script type="text/javascript"> var vglnk = { api_url: '//api.viglink.com/api', key: '89dcd0a12ff35d227eaaaff82503030b' }; (function(d, t) { var s = d.createElement(t); s.type = 'text/javascript'; s.async = true; s.src = ('https:' == document.location.protocol ? vglnk.api_url : '//cdn.viglink.com/api') + '/vglnk.js'; var r = d.getElementsByTagName(t)[0]; r.parentNode.insertBefore(s, r); }(document, 'script')); </script> ``` I want this code to be where I've put "HERE" ``` <html> <head> </head> <body> Some HTML and stuff HERE </body> </html> ``` How would I go about this in jQuery? (I'm doing this from an extension. Mainly in Chrome, but also Firefox and Internet Explorer.)

Original source

Related problems