2011年8月15日 星期一

[Windows] 初探 Browser Helper Object - 在特定條件下刪除 Cookie 為例

IE 附加元件


Browser Helper Object (BHO) 說穿了就像 Firefox Plugin 或 Google Chrome Extension 一樣,用來擴充瀏覽器的功能,只是實作上不同於 Firefox 跟 Chrome 的 Javascript 環境,這次使用的是 VC++ 的環境。沒想到人生中第一次實作瀏覽器 plugin 竟然是用在 IE 身上。


原先一直想用 Visual Studio Express 來實作,但裝了一陣子後,雖然可以透過 Windows Driver Kit (WDK) 來補齊 Active Template Library (ATL) 所需的 header file 或 library ,但最後看到的教學要新增一個 ATL Simple Object 時,卡關了,最後還是乖乖使用 VS 系列了。


關於範例就請直接參考 MSDN 官網:


Building Browser Helper Objects with Visual Studio 2005



來源:http://msdn.microsoft.com/en-us/library/bb250489(v=vs.85).aspx


官網寫得非常夠用!上述例子是當網頁讀完後(OnDocumentComplete),彈跳出一個視窗,如果熟悉 jQuery (或Javascript) 的話,就類似 $(document).ready(function(){ alert("Hello World!"); }); 效果。簡單地說,透過瀏覽器的事件偵測,可以玩很多好玩的東西。


由於周邊有人有這樣的需求,我就好奇播一點時間研究了一下。目前的需求:


在特定的 URL 下,刪除特定網域的 Cookie


這樣的行為基於安全性的設計,本來就是不允許的 :P 但透過安裝 BHO 後,可以刪除不同網域的 cookie 資料。所以啊,亂裝 BHO 是很危險的,並且 IE 又是 Windows 核心啊。


寫法只需更改 OnDocumentComplete(IDispatch *pDisp, VARIANT *pvarURL) 部分:


void STDMETHODCALLTYPE CHelloWorldBHO::OnDocumentComplete(IDispatch *pDisp, VARIANT *pvarURL)
{
    // Retrieve the top-level window from the site.
    HWND hwnd;
    HRESULT hr = m_spWebBrowser->get_HWND((LONG_PTR*)&hwnd);
    if (SUCCEEDED(hr))
    {
        // Output a message box when page is loaded.
        //MessageBox(hwnd, L"Hello World!", L"BHO", MB_OK);

        if ( wstrcmp( pvarURL->bstrVal , L"http://www.google.com.tw/" ) == 0 )
        {
            InternetSetCookie( "http://localhost" , "MyCookieName" , "MyCookieValue; expires=Thu, 01-Jan-1970 00:00:01 GMT" );
        }
    }
}


別忘了要引用相關函式庫:


#pragma comment(lib,"Wininet.lib")

#include <Wininet.h>


整個效果就是當瀏覽器瀏覽到 http://www.google.com.tw/ 時,對 http://localhost 網域進行設定 Cookie 的動作,由於設定的 expire time 已超過,等同於刪除指定的 cookie 資訊。這樣的清除 cookie 技巧可以用再強制幫使用者登出等等。


2011年8月10日 星期三

Building Webkit on Windows


圖片來源:http://www.webkit.org/


多年以前,總覺得 FreeBSD 系統的 make buildworld 很威,在 P-200 的電腦等級,編 FreeBSD 4 系列就花了 6 小時。而後隨著硬體的升級,並且在 VirtualBox 的環境中,大概都不用 30 分鐘了吧。一直以為這樣的等待只會出現在編譯 OS/Kernel 方面,如今在 Windows XP 上編譯 Webkit source code,沒想到浩浩蕩蕩地也等超過 30 分鐘 XD 看來我對 Windows 的軟體編譯經驗太弱了,殊不知很多大型程式碼,編譯時間也是很久的。


環境:


Windows XP with SP3
Visual Studio 2005 Express
Microsoft Platform SDK for Windows Server 2003 R2
Cygwin


Webkit 版本:


$ time svn checkout http://svn.webkit.org/repository/webkit/trunk WebKit
...
Checked out revision 92660.

real    79m43.055s
user    1m24.942s
sys     5m40.208s


步驟:


$ ~/Webkit
$ get http://developer.apple.com/opensource/internet/webkit_sptlib_agree.html
$ time ./Tools/Scripts/update-webkit
Updating OpenSource
...

real    6m53.662s
user    0m7.613s
sys     0m35.927s



$ time ./Tools/Scripts/build-webkit --debug
Installing WebKitSupportLibrary...
The WebKitSupportLibrary has been sucessfully installed in /home/user/WebKit/WebKitLibraries/win
Building results into: /home/user/WebKit/WebKitBuild
WEBKITOUTPUTDIR is set to: C:\cygwin\home\user\WebKit\WebKitBuild
WEBKITLIBRARIESDIR is set to: C:\cygwin\home\user\WebKit\WebKitLibraries\win
/cygdrive/c/Program Files/Microsoft Visual Studio 8/Common7/IDE/VCExpress.exe win\WebKit.vcproj\WebKit.sln /build Debug


錯誤訊息:


===== BUILD FAILED ======
Please ensure you have run Tools/Scripts/update-webkit to install dependencies.
You can view build errors by checking the BuildLog.htm files located at:
/home/user/WebKit/WebKitBuild/obj/<project>/<config>.
接著可在 ~/WebKit/WebKitBuild/Debug/buildfailed 看到哪個項目編譯有問題,再切換到該目錄裡尋找 BuildLog.htm 查看
例如 buildfailed 顯示 XXWebCoreXX,那就到 ~/WebKit/WebKitBuild/Debug/obj/WebCore/BuildLog.htm 查看即可。


相關筆記:


請先確認官方網站 BuildBot: WebKit,在這邊可以得知各個平台目前的狀況,有時編譯失敗不是自己環境的問題。


..\platform\DefaultLocalizationStrategy.cpp(345) : error C2001: 常數中包含新行字元
..\platform\DefaultLocalizationStrategy.cpp(347) : error C2001: 常數中包含新行字元
..\platform\DefaultLocalizationStrategy.cpp(345) : fatal error C1057: 巨集展開中未預期的檔案結尾


編輯 C:\cygwin\home\user\WebKit\Source\WebCore\platform\DefaultLocalizationStrategy.cpp
在 line 345 附近可以看到有使用全形“的地方,把他改成 \" 即可( "Look Up \"%@\"" 和 "Look Up \"<selection>\"")


error C2220: 將警告視為錯誤處理 - 沒有產生 'object' 檔案
warning C4819: 檔案含有無法在目前字碼頁 (950) 中表示的字元。請以 Unicode 格式儲存檔案以防止資料遺失


使用 VC++ 2005 Express 打開檔案後,再存檔一次即可,不需更動原始碼,即可解決問題。此 error C2220 問題可以參考 http://lists.macosforge.org/pipermail/webkit-help/2010-March/000860.html


以下是更動過得檔案
C:\cygwin\home\user\WebKit\Source\WebCore\platform\DefaultLocalizationStrategy.cpp
c:\cygwin\home\user\WebKit\Source\WebCore\css\CSSWrapShapes.h
C:\cygwin\home\user\WebKit\Source\WebCore\css\CSSWrapShapes.cpp
C:\cygwin\home\user\WebKit\Source\WebCore\rendering\RenderFlowThread.cpp
C:\cygwin\home\user\WebKit\Source\WebCore\rendering\RenderRegion.cpp
C:\cygwin\home\user\WebKit\WebKitLibraries\win\Include\ConditionalMacros.h


error PRJ0003 : 繁衍 (Spawn) 'midl.exe' 發生錯誤


將 C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Bin 加入 PATH 環境變數


LINK : fatal error LNK1104: 無法開啟檔案 'WebCoreTestSupport.lib'


原先是 DumpRenderTree 沒編過,而後才想起來可以找一下 WebCoreTestSupport 的狀態,在 WebKitBuild\###\obj\WebCoreTestSupport\BuildLog.htm,所以還是上面的老問題,把 WrapShapes.h 存檔一下就行啦


fatal error C1083: 無法開啟包含檔案: 'windows.h': No such file or directory


把 VC++ 2005 Express 打開,在 [工具] -> [選項] -> [專案與方案] -> [VC++ 目錄],將 "可執行檔"、"Include 檔案" 和 "程式庫檔",分別新增 C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2 對應的 Bin、Include 和 Lib 目錄


請把 How to: Use Visual C++ Express Edition with the Microsoft Platform SDK http://msdn.microsoft.com/en-us/library/ms235626(VS.80).aspx 做完。


至於執行編譯出來的 Webkit.exe 時,會碰到找不到相關的 dll 檔案,可以在下列位置確認一下:



2011年8月9日 星期二

[Javascript] 調整 TinyMCE 預設字型大小

上次隨意尋找,沒有看清楚,就荒廢了一陣子 XD


這次確實地找,看到相關的文章:



總之,解法就是可以在 tinyMCE.init 指定一個 content_css 資訊來設定顯示字型啦,例如:


@ /path/custom_content.css


body.mceContentBody {
    font-size:20px;
}


@ test.html


tinyMCE.init({
    ....
    content_css : "/path/custom_content.css?"+new Date().getTime(),
    ....
}); 


而 custom_contents.css 後面加上一個時間戳記,只是為了避免瀏覽器 cache 而已。


那些年擁抱的世界...


周末跟一位長輩聊天,聊著聊著就像說著那些年...


長輩說:「以前我們班的那些人,成績最好的當政府官員,接著來則是像我一樣當公司的員工,剩下來的要嘛不是老闆就是流氓。」


長輩的意思是要我更勇敢地衝刺,這就是為何那些成績不好的人會有兩種極端的結果,要嘛拼到老闆,要嘛就當流氓,千萬不要優柔寡斷,甚至騎驢找馬的下場就是什麼也沒有,傻傻地往前衝吧!


看著長輩談論著那些年,瞧得出他眼中的那些年的...未完成。


2011年8月3日 星期三

[Unix] 使用 Tarball 安裝 Nginx + PHP (FastCGI) + HTTPS @ FreeBSD 7

已經拖了將三過月的筆記,終於抽空寫完了。看了幾篇關於 Nginx 的文章,例如使用的資源比 Apache Web Server 少,以至於可以提供比 Apache 更多連線服務等。這次就順手安裝一下,雖然我對它還是不熟悉,就連 FastCGI 也是,只依稀知道要在 Apache Web Server 上跑 PHP,一種是用 mod_php 的方式,另一種好像就是 fastcgi 模式。網路上的文章是說 FastCGI 將 PHP 與 Web server 切開不綁在一起,使得 Web server 不會受 PHP 影響過深,也能達到節省資源。


此篇在 FreeBSD 7 環境下,採用 PHP-5.2.17、PHP-PFM 和 Nginx-1.0.5 並透過 tarball 方式進行安裝。可在到 Nginx 下載 nginx-1.0.5 stable version(Unix), 另外使用 PHP 5.2.17。而 FastCGI 的部份,有兩套管理方式,一種是使用 lighttpd 中的 spawn-fcgi ,另一種則是用 PHP-FPM ,在此選用 PHP-FPM 囉。


安裝:


下載 PHP-5.2.17:


> wget http://www.php.net/get/php-5.2.17.tar.gz/from/tw.php.net/mirror
> tar -xvf php-5.2.17.tar.gz


下載 PHP-PFM patch for PHP-5.2.17:


> wget http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz
> gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1


下載 Nginx-1.05:


> wget http://nginx.org/download/nginx-1.0.5.tar.gz
> tar -xvf nginx-1.0.5.tar.gz


安裝 PHP (依需求使用,但至少要有 --enable-fastcgi --enable-force-cgi-redirect --enable-fpm 資訊)


> cd php-5.2.17
>  ./configure --prefix=/path/tarball --without-pear --with-openssl --enable-mbstring --with-curl --with-imap --with-mcrypt --with-imap-ssl --with-gd --enable-pcntl --enable-fastcgi --enable-force-cgi-redirect --enable-fpm
> make install


安裝 Nginx:


> cd nginx-1.0.5
> ./configure --prefix=/path/tarball/ --with-http_ssl_module --with-pcre
> make install


設定:


PHP-FPM:


設定要跑 fastcgi 使用的 port number,預設在 9000:


> vim /path/tarball/etc/php-fpm.conf
<value name="listen_address">127.0.0.1:9000</value>


啟動 PHP-FPM:


> /path/tarball/sbin/php-fpm start
Starting php_fpm  done


停止:


> /path/tarball/sbin/php-fpm stop


Nginx :


設定 PHP FastCGI:


> vim /path/conf/nginx.conf
location ~ \.php$ {
#proxy_pass   http://127.0.0.1;
include conf/fastcgi.conf;
fastcgi_pass  127.0.0.1:9000;
fastcgi_index index.php;
}


設定 HTTPS:


建立憑證:


> cd /path/tarball/conf
> openssl genrsa -des3 -out server.key 2048
> openssl req -new -key server.key -out server.csr
> openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
> openssl rsa -in server.key -out server.key


設定 nginx.conf 之 HTTPS 部分:


> vim /path/conf/nginx.conf

在 http {} 中增加 client_max_body_size 30m; 來解決 413 Request Entity Too Large 訊息

# HTTPS server
#
server {
listen       443;
server_name  localhost;

ssl                  on;
ssl_certificate      server.crt;
ssl_certificate_key  server.key;

ssl_session_timeout  5m;

ssl_protocols  SSLv2 SSLv3 TLSv1;
ssl_ciphers  HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers   on;

location / {
root   html;
index  index.html index.htm index.php;
}
        location ~ \.php$ {
            #proxy_pass   http://127.0.0.1;
            include fastcgi.conf;
            fastcgi_param  HTTPS on; 
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
        }   
}


啟動:


> /path/tarball/sbin/nginx


停止:


> /path/tarball/sbin/nginx -s stop


如此一來,可以在 /path/tarball/html 目錄中,擺一個 index.php 檔案,用 <?php phpinfo(); ?> 來測試 PHP 是否正常工作囉。