如何获取扩展程序中文件的标准URL? [英] How do I get the fully qualified URL for a file inside my extension?

查看:82
本文介绍了如何获取扩展程序中文件的标准URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WebExtension API 来制作我的第一个Firefox扩展.并且找不到将网站重定向到扩展程序目录中的HTML文件的方法.

I am making my first Firefox extension using the WebExtension API and cannot find a way to redirect a website to an HTML file that is in the extension's directory.

我已经设法将网站重定向,但仅重定向到其他网站或网站上的其他页面.

I have already managed to redirect the website but only to other websites or other pages on the website.

manifest.json:

manifest.json:

{
    "manifest_version": 2,
    "name": "Focus",
    "version": "1.0",
    "permissions": [
        "activeTab"
    ],
    "browser_action": {
        "default_title": "Focus",
        "default_popup": "interface/main.html"
    },
    "content_scripts": [
        {
            "matches": ["*://*.reddit.com/*"],
            "js": ["redirect.js"]
        }
    ]
}

redirect.js:

redirect.js:

window.location = "interface/redirect.html";

当前代码会将您重定向到网站上的其他页面,例如如果您打开了reddit.com,它将带您进入reddit.com/interface/redirect.html

The current code will redirect you to another page on the website e.g. if you have reddit.com open it will take you to reddit.com/interface/redirect.html

我的目的是将您重定向到不需要在网站上托管的自定义HTML页面.

My aim is to redirect you to a custom HTML page that I do not need to host on a website.

推荐答案

您正在将页面的URL更改为相对于当前域的URL.扩展程序中的页面不在 reddit.com 域中.如果要使URL来自其他域,则必须指定域.在这种情况下,您需要使用 runtime.getURL() 获取标准网址:

You are changing the URL of the page to one that is relative to the current domain. The page in your extension is not in the reddit.com domain. If you want to the URL to be from a different domain, you have to specify the domain. In this case, you need to use runtime.getURL() to get the fully qualified URL:

Firefox或Chrome:

Firefox or Chrome:

window.location = chrome.runtime.getURL("interface/redirect.html");

或(仅限Firefox):

or (Firefox only):

window.location = browser.runtime.getURL("interface/redirect.html");

您还应该添加:

"run_at": "document_start"

到您的 manifest.json content_scripts条目.这样做会阻止页面显示,即使是很短的时间.换句话说,通过使用document_start,您的内容脚本将在呈现 redit.com 页面之前运行.

to your manifest.json content_scripts entry. Doing so will prevent the page from being displayed, even for a brief time. In other words, by using document_start, your content script will run prior to the redit.com page being rendered.

当您要重定向单个或几个预定义域时,使用内容脚本所采用的方法是一种合理的方法.这样会导致向用户报告的权限范围最小(即,仅matches中的域).

The approach you are taking, using a content script, is a reasonable one to use when you want to redirect a single, or a few, predefined domains. It will result in the least broad permissions being reported to the user (i.e. only the domains in your matches).

但是,对于重定向动态域列表而言,这还不够.在这种情况下,您应该使用 webNavigation.onBeforeNavigate 侦听器以执行导航的重定向.您还可以使用 webRequest 侦听器将所有流量重定向到一个或多个域.使用webRequest可用于阻止对域的所有请求,但与仅使用webNavigation.onBeforeNavigate阻止导航(如果选择,包括iframe)相比,它会占用更多的资源.使用哪种方法将取决于您要执行的操作.

However, for redirecting a dynamic list of domains, it is not sufficient. In that case, you should use a webNavigation.onBeforeNavigate listener to perform the redirection of the navigation. You could also use a webRequest listener to redirect all traffic to one or more domains. Using webRequest can be used to block all requests to the domain, but is more resource intensive than just blocking navigation using webNavigation.onBeforeNavigate (including iframes, if you choose). Which method you use will depend on what you are desiring to do.

您还可以使用tabs.onUpdated侦听器执行重定向,但是带有新URL的事件会在webNavigation.onBeforeNavigate和整个webRequest序列之后触发.换句话说,使用tabs.onUpdated侦听器仍将允许您向要重定向的域发出请求,已经创建了页面的DOM,并有可能(简短地)显示给用户.

You could also use a tabs.onUpdated listener to perform the redirection, but that event with the new URL fires after the webNavigation.onBeforeNavigate and the entire webRequest sequence. In other words, using a tabs.onUpdated listener will still allow the request to the domain you are redirecting to have been made, the DOM for the page to have been created and potentially shown (briefly) to the user.

这篇关于如何获取扩展程序中文件的标准URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆