To summarize the problem, I have several PDF files on my computer that contain links to other pages. Those links, however, direct you to the local filesystem instead of the internet (i.e. clicking the link opens the browser and takes you to file:///page
instead of http://domain/page
).
总而言之,我的计算机上有几个PDF文件,其中包含指向其他页面的链接。然而,这些链接会将您定向到本地文件系统,而不是互联网(即,单击链接将打开浏览器并将您带到file:///page,而不是http://domain/page).
Getting these files modified to include the full URL is not an option. My question is almost identical to the question I asked a year and a half ago. The difference is that I need to port my existing extension (made with the Firefox SDK) to the new WebExtensions API for Firefox (same as Chrome extensions).
不能将这些文件修改为包含完整的URL。我的问题与我一年半前提出的问题几乎完全相同。不同的是,我需要将我现有的扩展(用Firefox SDK制作)移植到新的用于Firefox的WebExages API(与Chrome扩展相同)。
There are methods available for redirection, such as
有可用于重定向的方法,例如
browser.webRequest.onBeforeRequest.addListener(
redirect,
{urls:[pattern]},
["blocking"]
);
but that only accepts http://
and https://
URL patterns.
但这只接受http://和https://URL模式。
I am currently trying to use the following code:
我目前正在尝试使用以下代码:
var id;
browser.tabs.onCreated.addListener( details => id = details.id )
browser.tabs.onUpdated.addListener( (tabId, changeInfo, tab) => {
var url = changeInfo.url;
if (tabId == id && url.includes("file:///")) {
url = url.replace("file:///page", http://domain/page");
browser.tabs.update(
id,
{ url: url }
);
}
});
Unfortunately, I have the same fundamental issue as with my original problem, resulting in the onUpdated
listener not firing (or if it does fire, it's not because of a URL change). But regardless of the listener used (e.g. onCreated
, onActivated
, etc.), I get about:blank
for the URL.
不幸的是,我遇到了与原来的问题相同的基本问题,导致onUpdated侦听器没有触发(或者即使触发了,也不是因为URL更改)。但不管使用的监听器(例如onCreated、onActivated等),我得到的是:空白的网址。
I have tried injecting code to change the value of the address bar, but that doesn't seem to work either:
我曾尝试通过注入代码来更改地址栏的值,但似乎也不起作用:
browser.tabs.executeScript( {
code: "window.location.href = window.location.href.replace('file:///', 'http://domain/')"
});
Thanks for any help!
感谢您的任何帮助!
更多回答
@DanielHerr I'm not sure how this is a duplicate. I don't need access to the file system, but rather the address bar. The URLs I happen to be working with have a file:///
scheme, but they're not valid URLs, so nothing on the local system is even accessed.
@DanielHerr我不确定这是什么复制品。我不需要访问文件系统,而是需要访问地址栏。我使用的URL恰好具有file:///方案,但它们不是有效的URL,因此本地系统上的任何内容都不会被访问。
Does adding file url permission not enable capability with request or navigation apis?
添加文件url权限不会启用请求或导航API的功能吗?
@DanielHerr Ah, I understand what you're saying. The problem seems to be identical to what I was facing in my original question I referenced from over a year ago. I wouldn't have this issue if the URL in question were something like file:///C:/path/to/file
. But it's not, it's file:///path/to/file
. Since it's not a valid URL, the browser sees it as an error and doesn't store it in any property or variable to be able to be used to trigger a listener.
@DanielHerr啊,我明白你的意思。这个问题似乎与我在一年多前引用的原始问题中所面临的问题相同。如果有问题的URL类似于file:///C:/path/to/file.,我就不会有这个问题但事实并非如此,这是file:///path/to/file.由于它不是有效的URL,浏览器会将其视为错误,并且不会将其存储在任何属性或变量中,以便能够用来触发侦听器。
我是一名优秀的程序员,十分优秀!