1

I have a script that I want to be injected into the document BEFORE load. I.E; it should function as if

<script..>MYSCRIPT HERE</script>

<html>
.
.

I have made a script in chrome that executes correctly (I can alert(0) etc) but, it runs in different environment which is not what I want. I want it to run in the SAME environment as page.

Earlier, I had used a trick where in the startup script I had used

window.location="javascript:<MY SCRIPT HERE>"

which effectively changes the execution environment but for the past few days it isn't working. I think its a chrome bug fix. Is there any other workaround for this? Note: I can't add script tags dynamically to the page in the startup script because the document.body etc are unavailable.

I would like to provide you more details of why I need this and my previous solution. HTML Page which I don't have control over:

.
.
<script>
function a(){//DOSOMETHING}
</script>
.
.
<script>
a(); <<------ I DONT WANT TO CALL THIS
</script>

Solution: startup JavaScript contains

window.location='javascript:const a=function(){};';<<-CONST used!!

That will force error of re-declaration of 'a' when the page actually loads, hence, when a() is called, nothing happens. (clever, right? -_-)

But now, I realized when I do window.location="js:..", even that runs in separate env!

halfer
  • 19,824
  • 17
  • 99
  • 186
Akshaya Shanbhogue
  • 1,438
  • 1
  • 13
  • 25

2 Answers2

3

You should be able to inject script into the page's JavaScript context by adding a script tag via your content script. In other words, your script could be injected at document_idle, and execute something like:

var s = document.createElement('script');
s.textContent = 'const a = function () {};';
document.documentElement.appendChild(s);

That script tag would be executed in the context of the page, not in the context of your script, and should allow you to achieve the result you're looking for.

Documentation for content scripts in general is available at https://developer.chrome.com/extensions/content_scripts

Xan
  • 74,770
  • 16
  • 179
  • 206
Mike West
  • 5,097
  • 25
  • 26
  • 1
    You should change your answer from "injected at document load" to "injected at document start" because thats specifically what he was asking about. Great stuff tho, works on document start (Ive wanted to know how to do that in the past). – PAEz Feb 14 '12 at 07:27
  • Fair point. I was sloppily using `load` to mean `idle`, but if you really need to inject script at the document's start, you absolutely have that option. :) I'll update my answer. – Mike West Feb 14 '12 at 08:44
  • Thanks for the reply, Mike, Tried the solution but It doesn't solve the problem :( Test: . . start script: s=document.createElement('script'); s.innerText='const a=function(){};'; document.documentElement.appendChild(s); It does get appended to body in the script but the function "a" is being re-declared. For ex, if I rename the function to "b", I can access "b" in the page using chrome script console, but "a" is being overwritten. Any way to prevent this behavior? – Akshaya Shanbhogue Feb 17 '12 at 17:03
  • If you can't read the above, click "edit". Not sure how to put code in comment in stackoverflow. Coming to the problem, specifically, on document start, if I put "const a=function(){}; alert(a);" << this alerts the empty function. However after document load, on web console alert(a) gives me the function declared in page. Reminder: This used to work a month back. Something is fishy.. – Akshaya Shanbhogue Feb 17 '12 at 17:12
  • 1
    Thinking its a bug, check the comments on this answer..... http://stackoverflow.com/questions/9347733/stop-a-function-from-execute-with-chrome-extension/9348685 – PAEz Feb 20 '12 at 16:48
0

Chrome probably has a bug in the current build as PAEz pointed out. But yes, a lot of insight on how things can be injected into "document" using chrome extension. Thanks Mike for the post. Will keep that in mind the next time I'm injecting using greesemonkey or something :)

Akshaya Shanbhogue
  • 1,438
  • 1
  • 13
  • 25