2011年11月21日星期一

Chrome Extension Isolation

I met a wield problem today, while I'm developing a chrome extension for our QAs.
I try to inject a script to click a group of specific links on the page. The script runs quite well when I paste it in firebug or chrome script console.
But once it is loaded from extension, it behaves in a strange way.
I spent several hours to figure out the problem. Finally I found the reason seems caused by jQuery function click event not fired correctly.
Then I try to debug the script line by line. And I found the code is indeed being executed. To confirm my assumption, I added an alert there, and it does be fired.
So I decide to publish the anchor object to a global variable(I mean a member defined on window).
Then I found a more wield phenomenon: when I debugger breaks in my script, the variable is being set correctly. But when I try to print the variable in console, the variable become undefined again!!!
I was confused by the result, then I guess the call might be muted by chrome for security reason. Then there should be some permission configuration somewhere.
But after I read every pages related to chrome extension permission configuration,  I still haven't found the answer I wish to found.
Suddenly I saw there are several words said  the extension is running in a isolated environment.
This inspired me that the chrome extension is working in a pattern that different than my understanding.
The content script doesn't manipulates the DOM directly but via a proxy and marshal.
Then it must be the reason why the even isn't fired, the event call couldn't be marshaled.
And the only solution is to inject the script to the page as a part of the DOM. Then the script can interact with the DOM directly.
Then I modified the manifest JSON, make it load a new script as content script, and I use that script insert a script tag to tag to load my original functionality script.
So as a conclusion: chrome extension is running in a isolated sandbox rather than running the DOM. We should remember this all the time, even though chrome makes the content script can access the Some directly. Since there are some limitations in such kinda simulation, such as function call and variable is not marshaled.

Sent from TimNew's Desire HD

Posted via email from 米良的实验室

没有评论:

发表评论