chrome http Request Header 修改插件

Posted 吃饭了吗

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了chrome http Request Header 修改插件相关的知识,希望对你有一定的参考价值。

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 修改插件的主要内容,如果未能解决你的问题,请参考以下文章

JSON REST Get-Request 正在等待用于 Chrome/Firefox 的 Tomcat 上的 CORS

Nginx http反向代理流程Proxy_pass模块

Chrome CLI 保存为 PDF 横向

HTTP入门:用Chrome开发者工具查看 HTTP 请求与响应

chrome谷歌浏览器安装yapi插件cross-request

chrome可以发送request