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 米良的实验室

2011年11月13日星期日

Simple Design

最近TW HR改變了Code Reivew的流程。現在所有應聘者的提交的代碼都會被上傳到一個SVN Server上去,然後大家 Review 完,通過 myTW 的投票機制打分。
其實這份代碼,麼一個 TWer 應該都有寫過,於是不知道從誰開始的,最近突然流行起了把自己當年面試時提交的代碼也上傳到SVN,然後讓大家幫忙 Review。

於是我也就跟風了一把~把我的代碼也搞上去了~
然後也看了看別人的代碼。

很有意思的是發現肖鵬的代碼有兩份,一份是當年面試提交的,另外一份應該是近期寫的。兩種完全不同的風格~
還有就是光磊的代碼,Java版,3個類搞定~

對比肖鵬的新版本和光磊的代碼~
我覺得我面試的那份代碼簡直就是 Over Design 和 Tricky Code 的絕佳範例!
一個小小的面試題裡,我居然實現了一個輕量級的依賴注入框架,一個輕量級的 RX 推送框架,以及一個 Message Bus 系統~
然後外部依賴也多到嚇人,xUnit,Moq, MVVM.Light, System.Windows.Interaction, Expression.Drawing, 最要命的是還有一個對 Code Contract的運行時編譯器 ccrewriter 的依賴,木有這玩意兒,不要想把代碼編譯過去!唯一值得慶幸的是,這些依賴大部分,能添加的都被我通過 Nuget 在編譯時進行自動下載了~

在看了光磊的極簡風格的代碼後,我就在想,當年的代碼風格確實是走上了一條不歸路。於是造就了這份連傳說中號稱無所不能的新哥也說看不懂的代碼~
現在看來,確實,這樣的代碼,不僅讓讀的人痛苦,也讓寫代碼的我無比痛苦~
最無語的,我現在已經不是很明白代碼的一些細節地方為什麼是那麼寫的了~而這時間,僅僅過去了幾個月而已~~

於是,我效仿,肖鵬同學,按照我現在的理解,寫了一個第二版出來。感謝 Scott Robinson 的那次169字符的打賭,在學習了無數超精簡的代碼版本後,我的第二版代碼也極大的精簡了。最讓我開心的是我徹底的在代碼裡去掉了傳說中的Switch Case~在Enumeration不支持定義方法和進行重載的 C#裡,我也寫出了近似於 Java 那樣精簡的代碼~ 當然,也利用了一些 Java 所沒有的 C# 特性~

最有意思的事情是,上次寫這個代碼,花了我整整三天,每天都還搞到 11 點半以後~(最後還是沒有足夠的時間去設計GUI的配置界面,於是及其 Tricky 通過在 WPF App 中重用了 Console 版的配置輸入解析模塊,於是造就那個從命令輸入參數,在GUI控制流程和渲染動畫的詭異應用~)
而這次,我僅僅花了4個小時,就搞定了所有的代碼~時間大概只花了原來的1/10~而且明顯代碼可讀性和可維護性都有了極大的提高~由於設計很簡單,所以可擴展性和靈活性在一定的修改前提下,也是不錯的~
相比之前那種通過大而全的框架去保證靈活性和可擴展性的方法來看,Simple Design 有著其非常獨特的優勢~

這是我第一次通過實踐,真正的見證了Simple Design 的強大~也讓我對這Simple Design的Simple有了更深一層的理解~Simple Design其實並不Simple~

Best regards,

TimNew
------------
If not now then when?
if not me then who?

Release your passion
To Realize your potential

I am a pessimist, I feel I'm living in a world without light.
But I am also a prayer, I believe I’m going towards a world full of sunshine!


Posted via email from 米良的实验室