使用FireFox扩展将URL从本地文件系统重定向到Internet [英] Redirect URL from local filesystem to internet with FireFox extension

查看:184
本文介绍了使用FireFox扩展将URL从本地文件系统重定向到Internet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的计算机上有几个PDF文件,其中包含指向其他页面的链接。然而,这些链接指向你的本地文件系统,而不是互联网。即点击该链接将打开浏览器并将您带到 file:/// page 而不是 http:// domain / page

修改这些文件以包含完整的URL不是一个选项。



我试过使用Firefox扩展重定向的URL,但都没有工作,所以我尝试创建自己的扩展做同样的。到目前为止我发现的是,在标签的准备好事件触发之前,URL是不可访问的,但是指向没有完整路径的本地文件的页面始终是未初始化的。



这里是我的扩展脚本,几乎来自 https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/tabs

  var tabs = require(sdk / tabs); 
tabs.on('open',function(tab){
tab.on('ready',function(tab){
if(tab.url.indexOf(file:/ // $ page $)
tab.url = tab.url.replace(file://,https:// domain);
}
});
});

任何想法如何将页面从本地文件重定向到另一个位置?

解决方案

最简单的方法是找到另一个StackOverflow答案... 获取地址栏的内容。使用该答案中的函数来检索URL,然后基于该URL进行重定向。所以我最终得到了以下结果:

$ $ p $ $ $ c $ var tab = require(sdk / tabs);
tabs.on('open',function(tab){
tab.on('activate',function(tab){
var {getMostRecentWindow} = require(sdk / window / );
var urlBar = getMostRecentWindow()。document.getElementById('urlbar');
if(urlBar.value.indexOf(file:/// page /)!= -1 ){
tab.url = urlBar.value.replace(file://,https:// domain);
}
});
}) ;


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.

I tried using available Firefox extensions to redirect the URL, but none worked, so I tried creating my own extension to do the same. What I've found so far is that the URL isn't accessible until the tab's "ready" event fires, but a page referring to a local file without the full path is always "uninitialized."

Here's my extension script, almost straight from https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/tabs:

var tabs = require("sdk/tabs");
tabs.on('open', function(tab){
    tab.on('ready', function(tab){
        if (tab.url.indexOf("file:///page") != -1) {
            tab.url = tab.url.replace("file://", "https://domain");
        }
    });
});

Any ideas how to go about redirecting a page from a local file to another location?

解决方案

The easiest way I've found to do this is actually from another StackOverflow answer... Get Content of Location Bar. Use the function in that answer to retrieve the URL and then redirect based on that. So I end up with the following:

var tabs = require("sdk/tabs");
tabs.on('open', function(tab){
    tab.on('activate', function(tab){
        var { getMostRecentWindow } = require("sdk/window/utils");
        var urlBar = getMostRecentWindow().document.getElementById('urlbar');
        if (urlBar.value.indexOf("file:///page/") != -1) {
            tab.url = urlBar.value.replace("file://", "https://domain");
        }
    });
});

这篇关于使用FireFox扩展将URL从本地文件系统重定向到Internet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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