chrome http Request Header 修改插件

2013-05-31 11:03:03|  分类: JavaScript |  标签:chrome  extensions  change  http  header  |举报|字号 订阅

 
 
  最近因为需要搞 Chrome 的浏览器插件,所以了解了 Chrome Extensions 的一些基本知识。Chrome 插件都可以在以下目中找到。

C:\Users\YouName\AppData\Local\Google\Chrome\User Data\Default\Extensions

  里面的文件夹名称是随机字符串,需要找到对应的可以打开 Chrome 中的,“工具”-->>“扩展程序”,里面去看对应插件的目录位置。
 
  先说一下程序的功能,程序的功能:当插件启用后,能够对指定范围的 urls request Header,进行修改。可以指定 Header 中,X-Forward-For,Referer,User-Agent,Cookie的值。也可以对Header中“X-Forward-For”的值(IP地址)随机生成。“Reffer”字段的值全部替换成请求的目标的URL值。用来做什么这里就不详细说了,自己发觉。
  下图是设置启用界面截图:
  这张是自动添加“X-Forward-For”,和修改“Reffer”,添加“Cookie”后的结果:可以看到请求的是 google Extensions 的网页,但是呢,Cookie 和 Referer 被我设置成为了我这里的博客地址。
  以下是程序的完成实现:
  1、先上一张目录结构说明图:

 
  2、然后是每个 Chrome 插件都有的 manifest.json 文件:

{
"name": "Change Request Header",
"description": "Change Http/Https Request Header!",
"version": "1.0",
"browser_action": {
"default_title": "Change Request Header",
"dafault_icon": "./icons/icon.png",
"default_popup": "./html/popup.html"
},
"icons": {
"16": "./icons/icon.png",
"32": "./icons/icon.png",
"48": "./icons/icon.png",
"128": "./icons/icon.png"
},
"background": {
"scripts": ["./js/background.js"]
},
"permissions": ["tabs", "webRequestBlocking", "webRequest", "<all_urls>"],
"manifest_version": 2
}

  3、popup.html 页面的设计:

<!DOCTYPE html>
<html>
<head>
<title>Change Request Header Setting</title>
<link href="../css/popup.css" rel="stylesheet" type="text/css">
<script src="../js/jquery.js"></script>
<script src="../js/popup.js"></script>
</head>
<body>
<div id="main">
<h3>Change Request Header Setting</h3>
<hr />
<div id="setting">
<span class="text">X-Forward-For:</span><input type="text" class="input" id="xForward" name="XForward" />&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" class="checkbox" id="randomip" /> Random IP<br />
<span class="text">Referer:</span><input type="text" class="input" id="referer" name="referer" />&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" class="checkbox" id="targeturl" /> Target URL<br />
<span class="text">User-Agent:</span><input type="text" class="input" id="userAgent" name="Uagent" /><br />
<span class="text">Cookie:</span><textarea class="input" cols="input" id="cookieArea" name="Cookie" placeholder="If the input is null, then it will use the origin value."></textarea>
<hr />
<input type="radio" id="exceptUrl" checked="checked"/> Except HOST: <input type="text" id="exceptUrltext" value=".google.com;.baidu.com;.qq.com" /><br />
<input type="radio" id="includeUrl" /> Include URL: <input type="text" id="includeUrltext" disabled="disabled" placeholder="Target site is ok" /><br />
<hr />
<div id="divButton">
<input type="button" class="button" id="enter" value="Enter" /><input type="button" class="button" id="reset" value="Reset" />
</div>
</div>
</div>
</body>
</html>

  4、popup.html 中的 css 文件可以按需修改,这里不贴了。然后重要的还有 popup.js 和 background.js 比较有用,这里发出来一下:
  popup.js:

$(document).ready(function() {
$('#xForward').val(localStorage['xForward']);
$('#referer').val(localStorage['referer']);
$('#userAgent').val(localStorage['userAgent']);
$('#cookieArea').val(localStorage['cookie']);

if(localStorage['isChange'] == 'true'){
$('#enter').attr({'disabled': true});
}

$('#randomip').change(function() {
//alert($('#randomip').is(':checked'));
$('#xForward').attr({'disabled':$('#randomip').is(':checked')});
$('#xForward').val('');
});
$('#targeturl').change(function() {
//alert($('#randomip').is(':checked'));
$('#referer').attr({'disabled':$('#targeturl').is(':checked')});
$('#referer').val('');
});
$('#exceptUrl').change(function() {
$('#includeUrl').attr({'checked': !$('#exceptUrl').is(':checked')});
$('#includeUrltext').attr({'disabled':$('#exceptUrl').is(':checked')});
$('#exceptUrltext').attr({'disabled':!$('#exceptUrl').is(':checked')});
});
$('#includeUrl').change(function() {
$('#exceptUrl').attr({'checked': !$('#includeUrl').is(':checked')});
$('#exceptUrltext').attr({'disabled':$('#includeUrl').is(':checked')});
$('#includeUrltext').attr({'disabled':!$('#includeUrl').is(':checked')});
});

$('#enter').click(function() {
var xForward = $('#xForward').val();
var referer = $('#referer').val();
var userAgent = $('#userAgent').val();
var cookie = $('#cookieArea').val();
var randomip = $('#randomip').is(':checked');
var targeturl = $('#targeturl').is(':checked');
var exceptUrl = $('#exceptUrl').is(':checked');
var includeUrl = $('#includeUrl').is(':checked');
var exceptUrltext = $('#exceptUrltext').val();
var includeUrltext = $('#includeUrltext').val();
localStorage['xForward'] = xForward;
localStorage['referer'] = referer;
localStorage['userAgent'] = userAgent;
localStorage['cookie'] = cookie;
localStorage['randomip'] = randomip;
localStorage['targeturl'] = targeturl;
localStorage['exceptUrl'] = exceptUrl;
localStorage['includeUrl'] = includeUrl;
localStorage['exceptUrltext'] = exceptUrltext;
localStorage['includeUrltext'] = includeUrltext;
localStorage['isChange'] = true;

$('#enter').attr({'disabled': true});
$('#reset').attr({'disabled':false});
})

$('#reset').click(function(){
$('#xForward').val('');
$('#referer').val('');
$('#userAgent').val('');
$('#cookieArea').val('');
$('#exceptUrltext').val('.google.com;.baidu.com;.qq.com');
$('#includeUrltext').val('');
$('#exceptUrl').attr({'checked':true});
$('#includeUrl').removeAttr('checked');

localStorage['xForward']='';
localStorage['referer']='';
localStorage['userAgent']='';
localStorage['cookie']='';
localStorage['randomip']=false;
localStorage['targeturl']=false;
localStorage['isChange']=false;
localStorage['exceptUrl']=true;
localStorage['includeUrl']=false;
localStorage['exceptUrltext']='.google.com;.baidu.com;.qq.com';
localStorage['includeUrltext']='';

$('#enter').attr({'disabled':false});
})
});

  background.js:

console.log('backgroud');
chrome.webRequest.onBeforeSendHeaders.addListener(function(details) {
var url = details.url;
if (isChange(url)) {
var headers = details.requestHeaders;
console.log("start request url:" + url);
var blockingResponse = modifyHeader(headers, url);
return blockingResponse;
}
}, {
urls : ["http://*/*", "https://*/*"]
}, ["requestHeaders", "blocking"]);

function isChange(_url){
var host = _url.split('/')[2];
if (localStorage['isChange'] != 'true'){
return false;
}
if (localStorage['exceptUrl'] == 'true'){
var exceptHost = localStorage['exceptUrltext'].split(';');
for(var i=0; i<exceptHost.length; i++){
var reg = exceptHost[i].replace(/\./g,'\\\.');
if(host.match(reg) != null){
return false;
}
}
}
else if (localStorage['includeUrl'] == 'true'){
var includeUrl = localStorage['includeUrltext'].split(';');
for(var i=0; i<includeUrl.length; i++){
var reg = includeUrl[i].replace(/\./g,'\\\.');
if(host.match(reg) == null){
return false;
}
}
}
return true;
}

function modifyHeader(_headers, _url){
var blockingResponse = {};
var isxForward = false;
var isReferer = false;
var isuserAgent = false;
var iscookie = false;
for (var j = 0; j < _headers.length; j++){
//console.log(_headers[j].name + ':' + _headers[j].value);
if(_headers[j].name == "X-Forward-For" && (localStorage['xForward'].length > 6 || localStorage['randomip'] == 'true')){
if(localStorage['xForward'].length > 6){
_headers[j].value = localStorage['xForward'];
}
else if (localStorage['randomip'] == 'true') {
_headers[j].value = Math.floor(Math.random()*225)+'.'+Math.floor(Math.random()*255)+'.'+Math.floor(Math.random()*255)+'.'+Math.floor(Math.random()*255);
}
isxForward = true;
}

if(_headers[j].name =="Referer"&&(localStorage['referer'].length >6|| localStorage['targeturl']=='true')){
//console.log('referer:'+_headers[j].value);
if(localStorage['referer'].length >6){
_headers[j].value = localStorage['referer'];
}
elseif(localStorage['targeturl']=='true'){
_headers[j].value = _url;
}
isReferer =true;
}

if(_headers[j].name =="User-Agent"&& localStorage['userAgent'].length >10){
_headers[j].value = localStorage['userAgent'];
isuserAgent =true;
}

if(_headers[j].name =="Cookie"&& localStorage['cookie'].length >0){
_headers[j].value = localStorage['cookie'];
iscookie =true;
}
}
if(isxForward ==false){
if(localStorage['xForward'].length >6){
_headers.push({name:'X-Forwarded-For',value:localStorage['xForward']});
//_headers['X-Forward-For'] = localStorage['xForward'];
}
elseif(localStorage['randomip']=='true'){
_headers.push({name:'X-Forwarded-For',value:Math.floor(Math.random()*225)+'.'+Math.floor(Math.random()*255)+'.'+Math.floor(Math.random()*255)+'.'+Math.floor(Math.random()*255)});
}
isxForward =true;
}
if(isReferer ==false){
if(localStorage['referer'].length >6){
_headers.push({name:'Referer',value:localStorage['referer']});
}
elseif(localStorage['targeturl']=='true'){
_headers.push({name:'Referer',value:_url});
}
isReferer =true;
}
if(isuserAgent ==false){
if(localStorage['userAgent'].length >10){
_headers['User-Agent']= localStorage['userAgent'];
}
isuserAgent =true;
}
if(iscookie ==false){
if(localStorage['cookie'].length >0){
_headers['Cookie']= localStorage['cookie'];
}
iscookie =true;
}
blockingResponse.requestHeaders = _headers;

console.log(blockingResponse);
return blockingResponse;
}

  
  完整的项目以及源代码可以参见:https://github.com/vipscu/Change-Header/tree/master/header/Cheader
  
  本文欢迎转载,转载请注明原文地址:http://vipscu.blog.163.com/blog/static/181808372201343053644567/

chrome http Request Header 修改插件的更多相关文章

  1. Request header is too large

    当request url过长的时候,经常tomcat后台就会包 "Request header is too large"错误. 解决方法:修改server.xml文件,linux ...

  2. HTTP Request header

    HTTP Request header HTTP协议详解 - 小坦克 - 博客园 HTTP Request header 当今web程序的开发技术真是百家争鸣,ASP.NET, PHP, JSP,Pe ...

  3. net::ERR_CONNCTION_ABORTED与http post request header is too large 错误

    开始浏览器报(net::ERR_CONNCTION_ABORTED)然后就一直找这个错误是怎么引起的,找了一圈也没有找到答案. 后来看了一下后台发出后台错http post request heade ...

  4. 解决Bug:Size of a request header field exceeds server limit

    用了cms 发现这玩意真不好,老是有各种奇芭的问题跳出来 有时浏览网页时会出现 Bad Request Your browser sent a request that this server cou ...

  5. Nginx里Header修改

    有时候,我们可能有修改Nginx默认Header的需求.本文就将常见的方法列出来供大家参考. 修改普通请求的Header Nginx内置的模块暂时仅支持修改响应头,使用add_header.其中: a ...

  6. 网页出现400 Bad Request Request Header Or Cookie Too Large错误的解决方法

    在开发项目过程中,突然遇到400 Bad Request Request Header Or Cookie Too Large的报错,我也是第一次出现这样的错误,感觉还是挺新奇的. 分析下出现错误的原 ...

  7. m_Orchestrate learning system---三十六、如何修改插件的样式(比如ueditor)

    m_Orchestrate learning system---三十六.如何修改插件的样式(比如ueditor) 一.总结 一句话总结:所有的js,html插件,修改样式无非是两种,一是直接修改css ...

  8. Feign请求中报错:Request header is too large 的解决方案。

    现在我们项目中都已迁入spring boot和spring cloud. 服务间调用现在都改成feign的调用方式,但是上次在实际使用过程中出现过:Request header is too larg ...

  9. java.lang.IllegalArgumentException: Request header is too large 解决方案

    错误描述: java.lang.IllegalArgumentException: Request header is too large 问题分析: 请求头超过了tomcat的限值.本来post请求 ...

随机推荐

  1. iOS学习笔记10-UIView动画

    上次学习了iOS学习笔记09-核心动画CoreAnimation,这次继续学习动画,上次使用的CoreAnimation很多人感觉使用起来很繁琐,有没有更加方便的动画效果实现呢?答案是有的,那就是UI ...

  2. 苹果Mac隐藏壁纸在哪里?Mac隐藏壁纸查找教程

    Mac隐藏壁纸怎么查找?Mac存不存在隐藏壁纸呢?今天小编就来解决大家的疑问,告诉大家怎么把Mac系统的隐藏壁纸找出来,并且弄能够正常的使用,小编特意写了一个详细的图文教程叫大家如何查找,使用Mac隐 ...

  3. mysql远程链接 方法和flush-hosts

    有时候会发现要用远程链接mysql 1 先要在mysql的host的机器上修改mysql表,最快就是复制一下本地localhost,现在phpmyadmin复制功能什么的很好用,然后把host列中的l ...

  4. Java中关于 BigDecimal 的一个导致double精度损失的"bug"

    背景 在博客 恶心的0.5四舍五入问题 一文中看到一个关于 0.5 不能正确的四舍五入的问题.主要说的是 double 转换到 BigDecimal 后,进行四舍五入得不到正确的结果: public ...

  5. c# 反射简单使用

    类库dll,将生成ExampleLib.dll文件 namespace ExampleLib { public class Example { public static string FuncA() ...

  6. Jquery Easy UI--datagrid的使用(转)

    第一篇学的是做一个管理的外框,接着就是数据datagrid绑定了,这里我用asp.net mvc3来做的,主要就是熟悉属性.方法. 打开easyui的demo 就可以看到如下一段代码: 和上篇一样cl ...

  7. Eclipse如何安装JD-Eclipse反编译插件

    一.Eclipse在线安装JD-Eclipse反编译插件 1.在eclipse的help—>Install New Software...中添加新软件开发,添加它的源: name:jd-ecli ...

  8. C++STL之迭代器

    迭代器 迭代器提供对一个容器中的对象的访问方法,并且定义了容器中对象的范围.迭代器就如同一个指针.事实上,C++的指针也是一种迭代器.但是,迭代器不仅仅是指针,因此你不能认为他们一定具有地址值.例如, ...

  9. ORA-01034: ORACLE not available如何解决

    一个小小的问题,让我折腾了一个上午,下午三点彻底解决了,分享一个给大家解决方法,尽管在测试服务器上,但是经验是值得总结和分享的. ERROR:ORA-01034: ORACLE not availab ...

  10. [转]jQuery Popup Login and Contact Form

    本文转自:http://www.formget.com/jquery-popup-form/ Pop up forms are the smart way to present your site. ...