chrome http Request Header 修改插件

chrome http Request Header 修改插件  

2013-05-31 11:03:03|  分類: JavaScript |  標籤:chrome  extensions  change  http  header  |舉報|字號 訂閱css

 
 
  最近由於須要搞 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值。用來作什麼這裏就不詳細說了,本身發覺。
  下圖是設置啓用界面截圖:
chrome http Request Header 修改插件 - vipscu - vipscu show
  這張是自動添加「X-Forward-For」,和修改「Reffer」,添加「Cookie」後的結果:能夠看到請求的是 google Extensions 的網頁,可是呢,Cookie 和 Referer 被我設置成爲了我這裏的博客地址。
chrome http Request Header 修改插件 - vipscu - vipscu show
 
  如下是程序的完成實現:
  一、先上一張目錄結構說明圖:
chrome http Request Header 修改插件 - vipscu - vipscu show
 
  二、而後是每一個 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
}
html


  三、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>
jquery


  四、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});
})
});
git

  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;
}
github

  
  完整的項目以及源代碼能夠參見: https://github.com/vipscu/Change-Header/tree/master/header/Cheader
  
  本文歡迎轉載,轉載請註明原文地址: http://vipscu.blog.163.com/blog/static/181808372201343053644567/
相關文章
相關標籤/搜索