彭旭 发表于 2012-12-30 16:04:59

[Chrome扩展]屏蔽Google搜索结果重定向

<div id="cnblogs_post_body">前言

在用Google搜索的时候,点击连接,Google不会直接进入目标网页,会先进入跳转页面(也就是重置或者重定向),使用<META http-equiv="refresh" content="0;URL='http://www.abc.com/'">再跳转过去。但是很多时候跳不过去,次数多了就烦了。最后终于想起来自己是个程序猿,得想办法屏蔽掉这个重置,直接进入目标网址。
就这个问题,我大概经历这么几个状态。
1. 直接在地址栏删掉无用的字符串,再挨个把编码后的://之类的字符挨个改回来(好2啊)。好吧,自己都不好意思说自己是程序猿。
2. 写一个uri解码的js,在html页里解码,然后再拷到地址栏(还是2)。
3. 开始想做个Chrome Extensions,把这个跳转给屏蔽了,折腾了半天做了个右键菜单。在跳转页面点右键再调过去。
4. 发现还有更简单的,在chrome.tabs.onUpdated增加监听,判断url是否符合google的跳转url格式。然后再处理跳转。用了几天感觉还行。
5. 过了几天升级Chrome后,发现manifest_version成2了,写的扩展用不了了。好吧,哥不折腾chrome.tabs.onUpdated了,我换chrome.webNavigation.onBeforeNavigate监听。
目前只针对google.com.hk的网页搜索,暂时来说是够用了。
Extensions

首先得起个名字,暂定NavTo。最终版的扩展的内容也就3个文件,一张图片、manifest.json、main.js。
然后再找一图片,作为Logo。
manifest.json

<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;"><div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">   1: {   2:   "name": "Navigate to",   3:   "version": "1.0",   4:   "manifest_version": 2,   5:   "description": "Navigating to the real URL",   6:   "icons": {"128": "icon128.png"},   7:   "permissions": ["webNavigation"],   8:   "background": {   9:         "scripts": ["main.js"]10:   }11: }
页: [1]
查看完整版本: [Chrome扩展]屏蔽Google搜索结果重定向