用自定义内容替换URL的工具

时间:2012-12-18 10:50:53

标签: google-chrome google-chrome-extension firefox-addon developer-tools

是否有开发工具或Chrome / Firefox扩展程序,您可以使用我自己的自定义内容替换特定网址的内容?

我不是在寻找一个简单的/ etc / hosts文件更改,因为我想要替换一个URL,而不仅仅是一个域。

2 个答案:

答案 0 :(得分:3)

在Chrome扩展程序中,webRequest API可用于将网址重定向到其他网址。此其他网址可以是其他在线网页,也可以是Chrome扩展程序中的网页 可以在this answer中找到webRequest API的简单示例以及其他选项列表。

注意:如果您只想在特定网页上添加/更改内容,Content scripts可能更合适。然后,您可以通过标准DOM方法更改特定页面的内容。

在Firefox中,您可以使用page-modAddon SDK模块来实现内容脚本。

答案 1 :(得分:1)

chrome.tab API可让您对窗口中的标签进行任何相关更改。

参考文献:

a) Tabs API

b) Basic Chrome Extension architecture.

您可以参考此示例扩展程序,将当前标签中的所有网址更改为Google.co.in

的manifest.json

这是一个核心文件,我们在其中注册所有Chrome扩展程序内容,确保它具有所有权限。

{
"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

涉及传递CSP的JS文件的简单HTML文件。

<!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);

如果您需要更多信息,请与我们联系。