Chrome extension script injection

becraig
becraig used Ask the Experts™
on
I need to figure out how to inject a script into a webpage via a Chrome extension.

I am able easily to use document.write / writeline but it seems to just sit there and do nothing I am sure I am doing something wrong and just too lazy to look for the right approach.

document.writeln('<Script>var prevent_bust = 0;window.onbeforeunload = function() {prevent_bust++}setInterval(function() {  if (prevent_bust > 0) {prevent_bust -= 2; window.top.location = "https://httpstat.us/204"}}, 1); </script>');
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Multitechnician
Top Expert 2014
Commented:
try this :
var s=document.createElement("script");s.type="text/javascript";s.text="var prevent_bust = 0;window.onbeforeunload = function() {prevent_bust++}setInterval(function() {  if (prevent_bust > 0) {prevent_bust -= 2; window.top.location = 'https://httpstat.us/204'}}, 1);";document.body.appendChild(s);

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial