Skip to content

Commit

Permalink
Adding full API replace
Browse files Browse the repository at this point in the history
  • Loading branch information
pasu authored and pasu committed Feb 27, 2020
1 parent 8fb8591 commit 8892297
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
42 changes: 29 additions & 13 deletions devtools/devtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ function checkURLTagged(url, replaceData) {
return replaceData.filter(replaceDatum => url.includes(replaceDatum.url));
}

function submitResponse(filteredData, continueParams) {
let responseLines = [];
if (filteredData.contentType) {
responseLines.push(`Content-Type: ${filteredData.contentType}`);
}
responseLines.push('');
responseLines.push('');
responseLines.push(filteredData.replace);
continueParams.rawResponse = btoa(unescape(encodeURIComponent(responseLines.join('\r\n'))));
chrome.debugger.sendCommand(debugee, 'Network.continueInterceptedRequest', continueParams);
}

let debugee = null;
function setupDebugger(target) {
debugee = { tabId: target.id };
Expand All @@ -51,21 +63,25 @@ function setupDebugger(target) {
if (filteredData.length > 0) {
var responseLines = [];
responseLines.push('HTTP/1.1 200 OK');
ajaxMe(request.url, (data) => {
replaceResponse(data.response, filteredData, (replacedData) => {
let headers = data.getAllResponseHeaders();
responseLines.push(headers);
responseLines.push('');
responseLines.push('');
responseLines.push(replacedData);
continueParams.rawResponse = btoa(unescape(encodeURIComponent(responseLines.join('\r\n'))));
if (filteredData[0].find === '~NO_API~') {
submitResponse(filteredData[0], continueParams)
} else {
ajaxMe(request.url, (data) => {
replaceResponse(data.response, filteredData, (replacedData) => {
let headers = data.getAllResponseHeaders();
responseLines.push(headers);
responseLines.push('');
responseLines.push('');
responseLines.push(replacedData);
continueParams.rawResponse = btoa(unescape(encodeURIComponent(responseLines.join('\r\n'))));
chrome.debugger.sendCommand(debugee, 'Network.continueInterceptedRequest', continueParams);
});
}, (status) => {
responseLInes[0] = `HTTP/1.1 ${status}`;
continecontinueParams.rawResponse = btoa(responseLines.join('\r\n'));
chrome.debugger.sendCommand(debugee, 'Network.continueInterceptedRequest', continueParams);
});
}, (status) => {
responseLInes[0] = `HTTP/1.1 ${status}`;
continecontinueParams.rawResponse = btoa(responseLines.join('\r\n'));
chrome.debugger.sendCommand(debugee, 'Network.continueInterceptedRequest', continueParams);
});
}
} else {
chrome.debugger.sendCommand(debugee, 'Network.continueInterceptedRequest', continueParams);
}
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Chrome Response Override",
"short_name": "Chrome Response Override",
"version": "1.2",
"version": "1.3",
"description": "Provides support to override API/HTML/* response using simple Find & Replace steps. No external app installation required",
"author": "Pasupathi Rajamanickam",
"devtools_page": "devtools/devtools.html",
Expand Down
6 changes: 5 additions & 1 deletion panel/panel.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
<input type="button" value="||" id="stop"/>
<input type="button" value="Save" id="save"/>
<input type="button" value="Clean" id="clean"/>
<input type="button" value="Add Row" id="addRow"/></td>
<input type="button" value="Add Row" id="addRow"/>
<a target="_blank" href="https://github.com/Pasupathi-Rajamanickam/chrome-response-override/wiki">Wiki</a>
</td>
</tr>
</table>

Expand All @@ -40,6 +42,8 @@
<th>URL Contains</th>
<th>Find</th>
<th>Replace</th>
<th>Content-Type</th>
<th>Delete</th>
</tr>
</table>
<div style="color:green" id="saveMessage"></div>
Expand Down
9 changes: 7 additions & 2 deletions panel/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function saveData() {
data.url = tds[0].getElementsByTagName('input')[0].value
data.find = tds[1].getElementsByTagName('input')[0].value
data.replace = tds[2].getElementsByTagName('input')[0].value
data.contentType = tds[3].getElementsByTagName('input')[0].value
if (data.url !== '' && data.find !== '' && data.replace !== '') {
replaceData.push(data);
}
Expand All @@ -59,10 +60,12 @@ function addRow() {
let cell2 = row.insertCell(1);
let cell3 = row.insertCell(2);
let cell4 = row.insertCell(3);
let cell5 = row.insertCell(4);
cell1.innerHTML = '<input type="text"/>';
cell2.innerHTML = '<input type="text"/>';
cell3.innerHTML = '<input type="text"/>';
cell4.innerHTML = '<input type="button" value="Delete" class="delete"/>';
cell4.innerHTML = '<input type="text"/>';
cell5.innerHTML = '<input type="button" value="Delete" class="delete"/>';
initDeleteEvent();
}

Expand Down Expand Up @@ -105,10 +108,12 @@ function initTable() {
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
cell1.innerHTML = `<input type="text" value="${escapeHTML(datum.url)}"/>`;
cell2.innerHTML = `<input type="text" value="${escapeHTML(datum.find)}"/>`;
cell3.innerHTML = `<input type="text" value="${escapeHTML(datum.replace)}"/>`;
cell4.innerHTML = `<input type="button" value="Delete" class="delete"/>`;
cell4.innerHTML = `<input type="text" value="${datum.contentType ? escapeHTML(datum.contentType) : ''}"/>`;
cell5.innerHTML = `<input type="button" value="Delete" class="delete"/>`;
});
initDeleteEvent();
} else {
Expand Down

0 comments on commit 8892297

Please sign in to comment.