代码改变世界

简单几行JS Code实现IE邮件转发新浪微博

2011-06-25 11:02  破狼  阅读(2658)  评论(0编辑  收藏  举报

     最近刚开通新浪围脖,突发奇想,想做个浏览器插件实现文字新闻等的转发围脖的插件。本以为应该用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>

看一个效果:

image

缺点:在这里只是简单获取的下文本值的内容,还没有找到获取选中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