0

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).

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).

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.

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.

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!

Community
  • 1
  • 1
Daniel H
  • 443
  • 3
  • 14
  • Possible duplicate of [Adding file://. permission to chrome extension](http://stackoverflow.com/questions/19493020/adding-file-permission-to-chrome-extension) – Daniel Herr Apr 24 '17 at 02:38
  • @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. – Daniel H Apr 24 '17 at 03:17
  • Does adding file url permission not enable capability with request or [navigation](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/webNavigation) apis? – Daniel Herr Apr 24 '17 at 03:22
  • @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. – Daniel H Apr 24 '17 at 03:44
  • Does [beforenavigate](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/webNavigation/onBeforeNavigate) not work? – Daniel Herr Apr 24 '17 at 03:45
  • No, it does not. – Daniel H Apr 24 '17 at 03:47

1 Answers1

0
redirectUrl : chrome.extension.getURL("hello.html")

Worked for me