利用chrome扩展使用HTTPS访问google
文章目录
源自我的博客
在国内访问google的时候,总是需要费一番周折,之前一直修改hosts,可以访问https://google.com.hk,后来发现总是出现连接被重置的情况,如下图:
而且可以发现,连接是http://google.com.hk, 如果改为https://google.com.hk还是可以正常访问的。但是每次手动修改就太麻烦了,因此想到可以用chrome的扩展程序来做这件事情。查阅了一些文档,发现chrome提供了很直接的接口,就是chrome.tabs.update方法,就是可以修改重定向的。主要代码如下:
manifest.json的代码:
{
"manifest_version": 2,
"name": "http2https",
"description": "visit google with https",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png"
},
"background":{
"page":"runjs.html"
},
"permissions":[
"tabs"
]
}
js的代码
var urlHistoryMap = [];
chrome.tabs.onUpdated.addListener(function(tabId, info, tab) {
if(!/http:\/\/.*?google(dservices)?.com/.test(tab.url)) return;
if(info.status == "loading") {
var preUrl = urlHistoryMap[tabId];
if(null == preUrl || preUrl != tab.url){
chrome.tabs.update(tab.id, {url: tab.url.replace("http://", "https://"),highlighted:true});
urlHistoryMap[tabId] = tab.url;
}
}
});
完整的代码:http2https
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论