最近剛開通新浪圍脖,突發奇想,想做個瀏覽器外掛程式實現文字新聞等的轉寄圍脖的外掛程式。本以為應該用C++寫個外掛程式的(c#那麼大個殼有時真的讓人頭疼),但是google了一下,手氣真的不錯,搜到了,幾篇關於IE外掛程式的開發。用的Javascript,和添加註冊表就可以完全搞定。外掛程式用javascript寫那就不用多說,瀏覽器本身就支援的,不用額外安裝。添加註冊表手動可以添加,但是麻煩所以寫了一個批處理搞定。
1:添加註冊表:HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt\<Menu Text> 設定預設值為html檔案路徑,添加值Contexts為)0xf3.
2:code : 在MSDN http://msdn.microsoft.com/en-us/library/aa753589(v=VS.85).aspx中有所講解。MSDN原文:Set the default value of the key to the URL of the page that contains the script you want the context menu entry to execute. This script can obtain the parent window object, the screen where the context menu item was executed, from the menuArguments property of the external object.
大概就是說我們可以用window.external.menuArguments這個對象擷取到內部的資訊,如window,document這些常用的對象。所以我們就可以馬上開工了,三下五去二不幾分鐘就搞定(代碼很簡單就直接Code了):
複製代碼 代碼如下:
<SCRIPT LANGUAGE = "JavaScript">
var oWindow = window.external.menuArguments;
var oDocument = oWindow.document;
var oSelect = oDocument.selection;
var oSelectRange = oSelect.createRange();
var sNewText = oSelectRange.text;//.substring(0,140);
var title=sNewText.length == 0?oDocument.title:sNewText;
if (title.length>0){
oWindow.open("http://service.weibo.com/share/share.php?title="+encodeURIComponent(title)+"&url="+encodeURIComponent(oDocument.URL)+"&source="+encodeURIComponent("破
狼")+"&appkey=1027675428&sourceUrl="+encodeURIComponent(oDocument.URL));
}
</SCRIPT>
看一個效果:
缺點:在這裡只是簡單擷取的下文本值的內容,還沒有找到擷取選中html或者對象的方式,正在尋找,如果有知道的請不吝賜教。我打算是可以解析出視頻,圖片等,發送。
下載【直接點擊批處理運行,重啟IE】
參考文獻:
http://msdn.microsoft.com/en-us/library/aa753589(v=VS.85).aspx
http://blogs.msdn.com/b/oldnewthing/archive/2004/05/24/140283.aspx