forked from Nitrama/HTTP-Header-Live
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTTPHeaderSub.js
executable file
·172 lines (166 loc) · 4.99 KB
/
HTTPHeaderSub.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
var TAB_ID = "";
var RESENDED_TAB = false;
var RESEND_TAB_NEW = "";
StorageChange();
function notify(request) {
//console.log(request.data)
data = request.data;
for (value in data) {
//checked
if (value == "onBeforeRequest") {
document.getElementById("select_method").value = data[value].method;
document.getElementById("header_url").value = data[value].url;
string = "";
if (data[value].requestBody != undefined) {
if (data[value].requestBody.formData != undefined) {
for (var i in data[value].requestBody.formData) {
string += i + "=" + data[value].requestBody.formData[i] + "&";
}
string = string.substr(0, string.length - 1);
} else if (data[value].requestBody.raw != undefined) {
utf8decode = new TextDecoder("utf-8");
string = utf8decode.decode(data[value].requestBody.raw[0].bytes);
}
}
document.getElementById("post_data").textContent = string;
document.getElementById("content_length_label").textContent =
chrome.i18n.getMessage("content_length_label") + string.length;
} else if (value == "onSendHeaders") {
string = "";
for (var i of data[value].requestHeaders) {
string += i.name + ": " + i.value + "\r\n";
}
document.getElementById("header_data").textContent = string;
} else if (value == "onAuthRequired") {
} else if (value == "onErrorOccurred") {
}
}
}
function replay_send() {
url = document.getElementById("header_url").value;
method = document.getElementById("select_method").value;
post = document.getElementById("post_data").textContent;
header = document.getElementById("header_data").textContent,
//console.log(header)
(temp_headers = header.replace(/\r\n/g, "<br>"));
temp_headers = temp_headers.split("<br>");
//console.log(temp_headers)
// The data we are going to send in our request
var myHeaders = new Headers();
for (temp_header of temp_headers) {
split = temp_header.split(": ", 2);
//console.log(split)
if (split != "") {
myHeaders.append(split[0], split[1]);
}
}
data = {
method: method
};
if (method != "GET") {
//console.log(post ,":",post.length,":", method)
myHeaders.delete("Content-type");
myHeaders.append(
"Content-type",
"application/x-www-form-urlencoded;charset=UTF-8"
);
//console.log(post ,":",post.length,":", method)
if (post.length != 0) {
data.body = post;
}
}
data.headers = myHeaders;
fetch(url, data)
.then(function(response) {
response.blob().then(function(data) {
objectURL = URL.createObjectURL(data);
//console.log(objectURL);
if (RESEND_TAB_NEW == true) {
if (RESENDED_TAB == true) {
tab_exists(objectURL);
} else {
RESENDED_TAB = true;
tab_create(objectURL);
}
} else {
tab_exists(objectURL);
}
});
})
.catch(function(err) {
console.error("Fetch Error:", err);
});
}
function tab_create(objectURL) {
chrome.windows.getAll(
{
windowTypes: ["normal"]
},
function(getwindows) {
//console.log(getwindows)
for (windows of getwindows) {
if (windows.type == "normal") {
WINDOW_ID = windows.id;
break;
}
}
chrome.tabs.create(
{
windowId: WINDOW_ID,
url: objectURL
},
function(tab) {
if (chrome.runtime.lastError) {
onError(chrome.runtime.lastError);
} else {
TAB_ID = tab.id;
}
}
);
}
);
}
function tab_exists(objectURL) {
chrome.tabs.get(TAB_ID, function() {
if (chrome.runtime.lastError) {
tab_create(objectURL);
} else {
chrome.tabs.update(
TAB_ID,
{
url: objectURL
},
function() {
//chrome.tabs.create({url: bloburl} , function (){
if (chrome.runtime.lastError) {
onError(chrome.runtime.lastError);
}
}
);
}
});
}
function StorageChange() {
//console.log("New Storage")
gettingItem = chrome.storage.local.get(function(item) {
if (item["new_tab_open"] == true) {
RESEND_TAB_NEW = true;
} else {
RESEND_TAB_NEW = false;
}
});
}
function onError(error) {
console.error("Error:", error);
alert("Error:" + error);
}
chrome.storage.onChanged.addListener(StorageChange);
chrome.runtime.onMessage.addListener(notify);
document
.getElementById("replay_send_button")
.addEventListener("click", replay_send);
document.getElementById("post_data").addEventListener("input", function() {
document.getElementById("content_length_label").textContent =
chrome.i18n.getMessage("content_length_label") +
document.getElementById("post_data").textContent.length;
});