遇到一个看不懂外文网页,我们第一反映就是装个翻译插件,或者把文字复制到翻译工具里。
其实不需要专门的浏览器或任何插件,只需一个书签,就可以实现网页翻译的功能:


这种书签叫 Bookmarklet,其内容并不是网址,而是一串 JavaScript 代码。通过下面三步就能添加一枚带翻译功能的书签(以 macOS 版 Safari 浏览器为例):
1. 任意选择一个网页,将其保存为书签;
2. 右键点击这枚书签,选择编辑地址;
3. 将地址替换为下面的代码(任选一个):
Google 翻译:
javascript:var%20t=((window.getSelection&&window.getSelection())||(document.getSelection&&document.getSelection())||(document.selection&&document.selection.createRange&&document.selection.createRange().text));var%20e=(document.charset||document.characterSet);if(t!=‘’){location.href=’http://translate.google.com/?text=‘+t+’&hl=zh-CN&langpair=auto|zh-CN&tbb=1&ie=‘+e;}else{location.href=’http://translate.google.com/translate?u=’+encodeURIComponent(location.href)+‘&hl=zh-CN&langpair=auto|zh-CN&tbb=1&ie=’+e;};
百度翻译:
javascript:(function(){window.open(‘http://fanyi.baidu.com/transpage?query=’+escape(document.location.href)+‘&from=auto&to=zh&source=url&render=1’)})();

添加好后,在一个外文网页直接点击这枚书签,就能实现翻译功能了。在电脑上比较简单,直接点书签栏中的图标即可,手机端需要先点地址栏展开书签页面后再点击。

一般来说,只要浏览器支持 JavaScript,Bookmarklet 就能够运行。主流的浏览器如 Chrome、Firefox、Safari 都支持 JavaScript,如果你发现运行失败,可以在浏览器设置里查看一下有没有开启 JavaScript。

> 下载 少数派 iOS 客户端、关注 少数派公众号,了解更多实用小技巧 💡