最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【记录】利用Notepad++的jN插件中的URL编码解码插件,实现从错误的google地址中提取原始url的功能

Notepad++ crifan 8676浏览 0评论

1.从为Notepad++ 添加url编/解码功能

知道了原来Notepad++是可以支持URL的编码和解码的。

参考其解释,去Notepad++插件主页去下载NppScripting,结果找不到该插件。

后来才发现,NppScripting早就改名为jN了。

插件下载地址是:jn.zip

2. 下载jn.zip后解压,将jN文件夹和jN.dll拷贝到Notepad++的plugin目录下,重启Notepad++,结果弹出一堆Syntax Error的js脚本错误:

syntax error line pos

但是去看了那些js脚本,却又找不到什么错误。

不过,加载完毕后,就可以看到工具栏上面多出了该插件中的js脚本所添加的那些菜单了:

added menus

后来才知道,相应的出错的js脚本所对应的功能,都被禁用或菜单变成灰色不可用了。

不过,毕竟我自己暂时只用到那个URL编解码的功能。

所以,此处为了简化问题,先把其他所有的脚本都移至jN\includes\disabled下,暂时都屏蔽再说。

3.然后拷贝了上面那位贴出的代码,由于其代码没有缩进,所以再去到这里:

http://jsbeautifier.org/

将其格式化一下,代码就好看多了。

再新建个urlEncDec.js,将代码放进去。

重启Notepad++,就可以看到对应菜单和其子菜单了。

4.然后突然想到了,对此URL的编解码,可以在其基础上,扩展一下,实现之前就想要通过IE9插件实现的那个功能:

【记录】为IE9编写一个插件AutoRedirectGoogleErrorUrl,用于对于,点击google搜索到的但点击时无法打开的地址,实现自动跳转到原始地址

即当打开google搜索出来的网页的地址不可用时,解码对应的google的地址,找到页面的原始地址。

然后就去看原先的此URL的编解码的代码,结果是对于Notepad++的Editor,currentView,text等变量操作的。

而此时自己想要获得其当前选择的内容,而非当前文件的全部的内容。

所以就去找这些变量是从哪里来的,即相关的API,结果搜了半天也没找到,后来才想起来,从Notepad++插件的原先地址:

http://sourceforge.net/apps/mediawiki/notepad-plus/index.php?title=Plugin_Central

中找到了jN的API函数:

http://www.softwarecanoe.de/jn/api.xml

然后找到了想要的,获得当前所选内容的变量:

selection VT_BSTR Gets or Sets Selected text in the view.

所以用法即为

Editor.currentView.selection

不过此处的Editor.currentView.selection貌似有bug,可以获得selection,但是无法设置selection,关于此点是否为bug,我已发邮件去问作者了。不知道其是否会回复偶,^_^。

反正经过一番折腾,写出代码如下:

var URLDecoderEncoder=Editor.addMenu("URL");
URLDecoderEncoder.addItem({
    //update: 2012-08-30
    //author: www.crifan.com
    text: "Decode+Extract+Copy Url from Google Error Url",
    cmd: function () {
        // got selected text, maybe multiline
        //refer: http://www.softwarecanoe.de/jn/api.xml#IDA1YK3B
        var selectedText = Editor.currentView.selection;

        //orig:
        //http://www.google.com.hk/url?sa=t&rct=j&q=&esrc=s&frm=1&source=web&cd=1&ved=0CCcQFjAA&url=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F11770443%2Fusing-uidatepicker-on-static-cell&ei=Idg-UNzaLpDqmAWDoIGwCg&usg=AFQjCNGP3C5aK7RBlrrxe981V8OpbIp15A&sig2=7QBNqN0p5koOz_2U7YVt0Q&cad=rjt

        var decodedSelection = decodeURIComponent(selectedText);
        //http://www.google.com.hk/url?sa=t&rct=j&q=&esrc=s&frm=1&source=web&cd=1&ved=0CCcQFjAA&url=http://stackoverflow.com/questions/11770443/using-uidatepicker-on-static-cell&ei=Idg-UNzaLpDqmAWDoIGwCg&usg=AFQjCNGP3C5aK7RBlrrxe981V8OpbIp15A&sig2=7QBNqN0p5koOz_2U7YVt0Q&cad=rjt

        //find xxx in &url=xxx&
        //var extractedUrl = decodedSelection.search(/&url=(.+?)&/);;
        //var extractedUrl = decodedSelection.match(/&url=(.+?)&/);;
        //refer:
        //http://www.w3school.com.cn/js/js_obj_regexp.asp
        //http://www.w3school.com.cn/js/jsref_obj_regexp.asp
        //http://www.2cto.com/kf/201204/126631.html
        var urlPattern = /&url=(.+?)&/i;
        var match = urlPattern.exec(decodedSelection);
        if(match)
        {
            var extractedUrl = match[1];
            //Editor.alert(extractedUrl);
            Editor.currentView.selection = extractedUrl; // not take effect ???
            //Editor.alert(Editor.currentView.selection);

            //add to clipboard
            //refer: http://www.softwarecanoe.de/jn/api.xml#IDA1YK3B
            System.clipBoard = extractedUrl;
        }
    }
});
URLDecoderEncoder.addItem({
    text: "Encode",
    cmd: function () {
        var unencoded = Editor.currentView.text
        var encoded = encodeURIComponent(unencoded);
        Editor.currentView.text = encoded;
    }
});
URLDecoderEncoder.addItem({
    text: "Decode",
    cmd: function () {
        var encoded = Editor.currentView.text
        var unencoded = decodeURIComponent(encoded);
        Editor.currentView.text = unencoded;
    }
});
URLDecoderEncoder.addItem({
    text: "Decode multi-pass (7x)",
    cmd: function () {
        var encoded = Editor.currentView.text
        var unencoded_pass1 = decodeURIComponent(encoded);
        var unencoded_pass2 = decodeURIComponent(unencoded_pass1);
        var unencoded_pass3 = decodeURIComponent(unencoded_pass2);
        var unencoded_pass4 = decodeURIComponent(unencoded_pass3);
        var unencoded_pass5 = decodeURIComponent(unencoded_pass4);
        var unencoded_pass6 = decodeURIComponent(unencoded_pass5);
        var unencoded = decodeURIComponent(unencoded_pass6);
        Editor.currentView.text = unencoded;
    }
});

使用方法简单介绍为:

1.当出现错误的google的页面的地址时,将其粘贴到Notepad++中,比如:

http://www.google.com.hk/url?sa=t&rct=j&q=&esrc=s&frm=1&source=web&cd=1&ved=0CCcQFjAA&url=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F11770443%2Fusing-uidatepicker-on-static-cell&ei=Idg-UNzaLpDqmAWDoIGwCg&usg=AFQjCNGP3C5aK7RBlrrxe981V8OpbIp15A&sig2=7QBNqN0p5koOz_2U7YVt0Q&cad=rjt

2.选中此google页面地址

3.点击URL->Decode+Extract+Copy Url from Google Error Url:

select and decode and extract

4.即可获得解码后,提取出来的地址了:

http://stackoverflow.com/questions/11770443/using-uidatepicker-on-static-cell

extracted url

同时,也已经将此URL地址复制到系统剪贴板中。

方便使用者直接粘贴到浏览器中,去打开该页面了。

 

【总结】

使用Notepad++的插件jN,以后可以写更多的js脚本,实现更多的功能了。


【后记 2012-09-30】

其实关于前面提到的,发邮件给作者的事情,作者Eugen Kremer很快就回信了。给偶解释了,此问题,不是bug,而是“feature”。

然后也给出了相关的实现鼠标选中内容的代码,最终的URL.js的代码为:

var URLDecoderEncoder=Editor.addMenu("URL");
URLDecoderEncoder.addItem({
    //update: 2012-09-30
    //author: www.crifan.com
    text: "Decode+Extract+Copy Url from Google Error Url",
    cmd: function () {
        // got selected text, maybe multiline
        //refer: http://www.softwarecanoe.de/jn/api.xml#IDA1YK3B
        var selectedText = Editor.currentView.selection;
 
        //orig:
        //http://www.google.com.hk/url?sa=t&rct=j&q=&esrc=s&frm=1&source=web&cd=1&ved=0CCcQFjAA&url=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F11770443%2Fusing-uidatepicker-on-static-cell&ei=Idg-UNzaLpDqmAWDoIGwCg&usg=AFQjCNGP3C5aK7RBlrrxe981V8OpbIp15A&sig2=7QBNqN0p5koOz_2U7YVt0Q&cad=rjt
 
        var decodedSelection = decodeURIComponent(selectedText);
        //http://www.google.com.hk/url?sa=t&rct=j&q=&esrc=s&frm=1&source=web&cd=1&ved=0CCcQFjAA&url=http://stackoverflow.com/questions/11770443/using-uidatepicker-on-static-cell&ei=Idg-UNzaLpDqmAWDoIGwCg&usg=AFQjCNGP3C5aK7RBlrrxe981V8OpbIp15A&sig2=7QBNqN0p5koOz_2U7YVt0Q&cad=rjt
         
        //find xxx in &url=xxx&
        //var extractedUrl = decodedSelection.search(/&url=(.+?)&/);;
        //var extractedUrl = decodedSelection.match(/&url=(.+?)&/);;
        //refer:
        //http://www.w3school.com.cn/js/js_obj_regexp.asp
        //http://www.w3school.com.cn/js/jsref_obj_regexp.asp
        //http://www.2cto.com/kf/201204/126631.html
        var urlPattern = /&url=(.+?)&/i;
        var match = urlPattern.exec(decodedSelection);
        if(match)
        {
            var extractedUrl = match[1];
            //Editor.alert(extractedUrl);
            
			// use byte(Pos|Anchor) for better performance
			// store position of selection beginning 
            var selectionBeginning = currentView.bytePos < currentView.byteAnchor ? currentView.bytePos : currentView.byteAnchor;
			
            Editor.currentView.selection = extractedUrl;
			
            currentView.bytePos    = selectionBeginning ;
			currentView.byteAnchor = selectionBeginning ;
            // use character anchor because of possible unicode characters in extractedUrl
			currentView.anchor    += extractedUrl.length;
			 
            //Editor.alert(Editor.currentView.selection);
             
            //add to clipboard
            //refer: http://www.softwarecanoe.de/jn/api.xml#IDA1YK3B
            System.clipBoard = extractedUrl;
        }
    }
});
URLDecoderEncoder.addItem({
    text: "Encode",
    cmd: function () {
        var unencoded = Editor.currentView.text
        var encoded = encodeURIComponent(unencoded);
        Editor.currentView.text = encoded;
    }
});
URLDecoderEncoder.addItem({
    text: "Decode",
    cmd: function () {
        var encoded = Editor.currentView.text
        var unencoded = decodeURIComponent(encoded);
        Editor.currentView.text = unencoded;
    }
});
URLDecoderEncoder.addItem({
    text: "Decode multi-pass (7x)",
    cmd: function () {
        var encoded = Editor.currentView.text
        var unencoded_pass1 = decodeURIComponent(encoded);
        var unencoded_pass2 = decodeURIComponent(unencoded_pass1);
        var unencoded_pass3 = decodeURIComponent(unencoded_pass2);
        var unencoded_pass4 = decodeURIComponent(unencoded_pass3);
        var unencoded_pass5 = decodeURIComponent(unencoded_pass4);
        var unencoded_pass6 = decodeURIComponent(unencoded_pass5);
        var unencoded = decodeURIComponent(unencoded_pass6);
        Editor.currentView.text = unencoded;
    }
});

转载请注明:在路上 » 【记录】利用Notepad++的jN插件中的URL编码解码插件,实现从错误的google地址中提取原始url的功能

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (3)

  1. 学习啦。。不过关于那个google跳转原来的页面实用性不强啊。。。习惯用firefox。。。有脚本可以实现的,直接jump到网页的源地址。。。免除了粘贴又解码的麻烦。。。嘿嘿嘿。。
    艳文12年前 (2012-08-30)回复
    • google跳转的功能,只是我个人觉得有用,其他人的确是未必会用到。呵呵。 不需要用到的,自己去js中把对应代码删除就可以了。
      crifan12年前 (2012-08-31)回复
87 queries in 0.177 seconds, using 22.14MB memory