pig*_*ack 2 javascript jquery google-chrome-extension
我为facebook写了一个发送数据的插件testing-fb.local.如果用户已登录,则请求将通过.
这是工作流程:
testing-fb.local$.ajax()从Chrome扩展程序中触发
chrome.webRequest.onBeforeSendHeaderschrome.cookies.getSet-Cookies要发送的标头请求通过.
我写了这部分代码,应该是这样的:
function getCookies (callback) {
chrome.cookies.get({url:"https://testing-fb.local", name: "connect.sid"}, function(a){
return callback(a)
})
}
chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {
getCookies(function(a){
// Here something happens
})
},
{urls: ["https://testing-fb.local/*"]},
['blocking']);
Run Code Online (Sandbox Code Playgroud)
这是我的manifest.json:
{
"name": "test-fb",
"version": "1.0",
"manifest_version": 1,
"description": "testing",
"permissions": [
"cookies",
"webRequest",
"tabs",
"http://*/*",
"https://*/*"
],
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["http://*.facebook.com/*", "https://*.facebook.com/*"],
"exclude_matches" : [
"*://*.facebook.com/ajax/*",
"*://*.channel.facebook.tld/*",
"*://*.facebook.tld/pagelet/generic.php/pagelet/home/morestories.php*",
"*://*.facebook.tld/ai.php*"
],
"js": ["jquery-1.8.3.min.js", "allthefunctions.js"]
}
]
}
Run Code Online (Sandbox Code Playgroud)
在allthefunction.js我有$ .ajax调用,并在background.js我把上面的代码,但看起来不运行的地方..
总之,我还不清楚:
Chrome changes the Set-Cookies header to be sent,我相信您要为HTTP请求发送自定义Cookie标头,因为 服务器发送Set-Cookie标头以响应HTTP请求,该请求用于在用户系统上创建cookie.
请注意所有Cookie API()方法都是异步的,确保您的回调驱动您的功能;
我尝试使用此示例演示复制您的工作流程
示范
后台页面查找对http://www.w3schools.com/html/default.asp的所有请求并修改cookie详细信息
background.js
// Adding Event Listener for Changing Cookie Details in Header
chrome.webRequest.onBeforeSendHeaders.addListener(function (details) {
// Look for header details here
detail = "requestHeaders";
headers = details[detail];
// Iterate through all headers information
for (header in headers) {
// Stop at Cookie Information
if (headers[header].name == "Cookie") {
// Do your desired functionality either modifying/delet cookies etc
//chrome.cookies.get({url:"<your url>", name: "<your parameter>"}, function(Cookies){
//console.log("Cookies "+Cookies);
return {
requestHeaders: details.requestHeaders
};
//});
}
}
},
//Sample URL used
{
urls: ["http://www.w3schools.com/html/default.asp"]
},
// Block Web Request and request for header information
['blocking', "requestHeaders"]);
Run Code Online (Sandbox Code Playgroud)
allfunctions.js
简单的AJAX调用asp页面
//Do an AJAX Call
function ajaxCall() {
$.post("http://www.w3schools.com/html/default.asp", function (data) {
console.log("POST Completed " + data);
});
}
ajaxCall();
Run Code Online (Sandbox Code Playgroud)
的manifest.json
确保清单具有所有权限
{
"name": "test-fb",
"version": "1.0",
"manifest_version": 2,
"description": "testing",
"permissions": [
"cookies",
"webRequest",
"tabs",
"<all_urls>",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["jquery.js", "allthefunctions.js"]
}
]
}
Run Code Online (Sandbox Code Playgroud)
有了这个,我能够跟踪AJAX Call的请求标头并修改cookie信息,如果您需要更多信息,请告诉我.