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. sublime text 3 如何安装 package control

    sublime text3 是个很好的编辑工具,前端程序员觉得她很好,我是在一次视频中看到她能帮助自动完成很多快捷的操作. 为什么安装? 如果想要给sublime text 中安装别的插件(这里称呼为 ...

  2. 《SQL Server企业级平台管理实践》读书笔记——几个系统库的备份与恢复

    master数据库 master作为数据库的主要数据库,记录着SQL Server系统的所有系统级信息,例如登录用户.系统配置设置.端点和凭证以及访问其他数据服务器所需要的信息.master数据库还记 ...

  3. 基于python的flask的应用实例注意事项

    1.所有的html文件均保存在templates文件夹中 2.运行网页时python manage.py runserver

  4. Eclipse中使用Working Set来管理项目

    Eclipse作为一款流行的JavaIDE开发工具,其有很多好用的功能为我们的开发提供帮助.但我们的工作空间中有很多项目时,管理起来就很头疼了. 但是我们又不想更换工作区间,所以我们需要一个更加有效的 ...

  5. python 解析json loads dumps

    认识 引用模块 重要函数 案例 排序 缩进参数 压缩 参考 认识 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standa ...

  6. C++find函数

    头文件 #include <algorithm> 函数实现 template<class InputIterator, class T> InputIterator find ...

  7. Linked List Cycle

    Given a linked list, determine if it has a cycle in it. /** * Definition for singly-linked list. * s ...

  8. Hadoop with tool interface

    Often Hadoop jobsare executed through a command line. Therefore, each Hadoop job has to support read ...

  9. Linux 系统常用命令汇总(七) 安全设置

     安全设置 分类 命令 选项 注解 安全策略 selinux setenforce number(0,1) 设置selinux状态,0关闭,1开启 getenforce 显示当前selinux状态,e ...

  10. UESTC 913 握手 Havel定理+优先队列

    给定一个非负整数序列{dn},若存在一个无向图使得图中各点的度与此序列一一对应,则称此序列可图化.进一步,若图为简单图,则称此序列可简单图化. 此题因为是无自环无重边,所以是简单图.用判定简单图可图化 ...