1

Is there a development tool or Chrome / Firefox extension that lets you replace the contents of a specific URL with my own custom content?

I'm not looking for a simple /etc/hosts file change, because I want to replace a URL, not just a domain.

Eduard Luca
  • 6,514
  • 16
  • 85
  • 137

2 Answers2

3

In a Chrome extension, the webRequest API can be used to redirect URLs to a different URL. This other URL can be another online page, but also a page within your Chrome extension.
A simple example for the webRequest API, and a list of other options can be found in this answer.

Note: If you just want to add / change something on a specific page, Content scripts may be more suitable. You can then change the content of a specific page by standard DOM methods.

In Firefox, you can use the page-mod module of the Addon SDK to implement the Content script.

Community
  • 1
  • 1
Rob W
  • 341,306
  • 83
  • 791
  • 678
1

chrome.tab API will let you to make any related changes to a tab in a window.

References:

a) Tabs API

b) Basic Chrome Extension architecture.

You can refer this Sample Extension for changing all URL's in a current tab to Google.co.in

manifest.json

This is a core file where we register all chrome extension content, ensure it is with all permissions.

{
"name":"Tabs Demo",
"description":"This Demonstrates Demo of Tabs",
"browser_action":{
"default_icon":"screen.png",
"default_popup":"popup.html"
},
"permissions":["tabs"],
"manifest_version":2,
"version":"1"
}

popup.html

Trivial HTML file referring to a JS file to pass CSP.

<!doctype html>
<html>
<head>
<script src="popup.js"></script>
</head>
<body>
</body>
</html>

popup.js

function tabsfunction() {
    //fetching all tabs in window
    chrome.tabs.getAllInWindow(function (tabs) {
        // Iterating through tabs
        for (tab in tabs) {
            //Updating each tab URL to custom url 
            chrome.tabs.update(tabs[tab].id, {
                "url": /*You can place any URL you want here*/
                "https://www.google.co.in/"
            }, function () {
                //Call back
                console.log("Completed");
            });
        }
    });
}
//Binging a function on document events 
document.addEventListener("DOMContentLoaded", tabsfunction);

Let me know if you need more information.

Sudarshan
  • 18,140
  • 7
  • 53
  • 61
  • The problem is that the content I want replaced is being loaded from within a JavaScript, so I'm not sure your solution will work for that. Would it? – Eduard Luca Dec 18 '12 at 13:07
  • @EduardLuca:Yes, it will work, if you can provide related code of your java script i can show that as well.. – Sudarshan Dec 18 '12 at 15:11
  • 1
    Found something that goes along with what you said, but didn't do exactly what I wanted, so I took that and modified it and got my stuff working. Thanks! – Eduard Luca Dec 19 '12 at 12:36