Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extension, replace HTML in response code before browser displays it

i wonder if there is some way to do something like that: If im on a specific site i want that some of javascript files to be loaded directly from my computer (f.e. file:///c:/test.js), not from the server.

For that i was thinking if there is a possibility to make an extension which could change HTML code in a response which browser gets right before displaying it. So whole process should look like that:

  1. request is made
  2. browser gets response from server
  3. #response is changed# - this is the part when extension comes in
  4. browser parse changed response and display page with that new response.

It doesnt even have to be a Chrome extension anyway. It should just do the job described above. It can block original file and serve another one (DNS/proxy?) or filter whole HTTP traffic in my computer and replace specific code to another one of matched response.

like image 214
lukasz Avatar asked Jun 07 '11 09:06

lukasz


People also ask

Can Chrome extensions edit HTML?

Page Edit is an extension that let you make changes to any HTML webpage. To work with this add-on, simply open the toolbar popup UI and then click on the big toggle button at the left side. Once the add-on is active, the icon turns to blue color.

How do I insert HTML code into Chrome?

Assuming Google Chrome is set as your default program, you should simply double tap the HTML document and it will naturally open in Chrome.

Are Chrome extensions detectable?

“Chrome extensions can be detected by fetching their web-accessible resources — the files inside an extension that web pages can access,” z0ccc explained. “The detected extensions can be used to track you through browser fingerprinting.”


2 Answers

You can use the WebRequest API to achieve that. For example, you can add a onBeforeRequest listener and redirect some requests:

chrome.webRequest.onBeforeRequest.addListener(function(details)
{
  var responseData = "<div>Some text</div>"
  return {redirectUrl: "data:text/html," + encodeURIComponent(responseData)};
}, {urls: ["https://www.google.com/"]}, ["blocking"]);

This will display a <div> element with the text "some text" instead of the Google homepage. Note that you can only redirect to URLs that the web server itself is allowed to redirect to. This means that redirecting to file:/// URLs is not possible, and you can only redirect to files inside your extension if these are web accessible. data: and http: URLs work fine however.

like image 200
Wladimir Palant Avatar answered Oct 19 '22 10:10

Wladimir Palant


In Windows you can use the Proxomitron (proxomitron.info) which is a local proxy that can intercept any page or file being loading into your browser and change it using regular expressions (no DOM parsing) however you want, before it is rendered by the browser.

like image 28
Steve Avatar answered Oct 19 '22 09:10

Steve