XUL 使用当前打开选项卡中解析的 URI 打开新选项卡

发布于 2024-11-03 08:05:27 字数 3444 浏览 5 评论 0原文

我正在使用的 javascript 函数似乎没有响应,或者我在某处的语法不正确。我使用的 Parse_URL 函数来自 http://phpjs.org/functions/parse_url:485

我想要做的是;从当前打开的选项卡;获取 URL,解析它,然后将该 URL 传递给另一台服务器上的脚本(在新选项卡中)

我设法让它打开一个新选项卡.. 但我尝试解析 URL 的每种方法都会很短。

这是我当前的代码:

CheckWhois = {

1: function () {
//parse this URI
function parse_url (str, component) {
// http://kevin.vanzonneveld.net
// +      original by: Steven Levithan (http://blog.stevenlevithan.com)
// + reimplemented by: Brett Zamir (http://brett-zamir.me)
// + input by: Lorenzo Pisani
// + input by: Tony
// + improved by: Brett Zamir (http://brett-zamir.me)
// %          note: Based on http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
// %          note: blog post at http://blog.stevenlevithan.com/archives/parseuri
// %          note: demo at http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
// %          note: Does not replace invalid characters with '_' as in PHP, nor does it return false with
// %          note: a seriously malformed URL.
// %          note: Besides function name, is essentially the same as parseUri as well as our allowing
// %          note: an extra slash after the scheme/protocol (to allow file:/// as in PHP)
// *     example 1: parse_url('http://username:password@hostname/path?arg=value#anchor');
// *     returns 1: {scheme: 'http', host: 'hostname', user: 'username', pass: 'password', path: '/path', query: 'arg=value', fragment: 'anchor'}
var key = ['source', 'scheme', 'authority', 'userInfo', 'user', 'pass', 'host', 'port', 
                    'relative', 'path', 'directory', 'file', 'query', 'fragment'],
    ini = (this.php_js && this.php_js.ini) || {},
    mode = (ini['phpjs.parse_url.mode'] && 
        ini['phpjs.parse_url.mode'].local_value) || 'php',
    parser = {
        php: /^(?:([^:\/?#]+):)?(?:\/\/()(?:(?:()(?:([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?()(?:(()(?:(?:[^?#\/]*\/)*)()(?:[^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
        strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
        loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // Added one optional slash to post-scheme to catch file:/// (should restrict this)
    };

var m = parser[mode].exec(str),
    uri = {},
    i = 14;
while (i--) {
    if (m[i]) {
      uri[key[i]] = m[i];  
    }
}

if (component) {
    return uri[component.replace('PHP_URL_', '').toLowerCase()];
}
if (mode !== 'php') {
    var name = (ini['phpjs.parse_url.queryKey'] && 
            ini['phpjs.parse_url.queryKey'].local_value) || 'queryKey';
    parser = /(?:^|&)([^&=]*)=?([^&]*)/g;
    uri[name] = {};
    uri[key[12]].replace(parser, function ($0, $1, $2) {
        if ($1) {uri[name][$1] = $2;}
    });
}
delete uri.source;
return uri;
 }
var URI = currentBrowser.currentURI.spec;

//unsure if this will work
var tab = document.getElementById("content").addTab("tools.whois.com.au/whois/?domain=" . parse_url("test.com", 'host'));
document.getElementById("content").selectedTab = tab;
document.getElementById("urlbar").focus();

  },

}

我不习惯 javascript,这绝对是一个菜鸟问题:)

任何帮助将不胜感激:)

The javascript function I am using seems to not respond or I have incorrect syntax somewhere. The Parse_URL function I use is from http://phpjs.org/functions/parse_url:485.

What I want to be able to do is; from the currently open tab ; take the URL, parse it and then pass that url along to a script on another server( in a new tab)

I managed to get it to open a new tab..
but every method i try to parse the URL comes up short..

Here is my current code:

CheckWhois = {

1: function () {
//parse this URI
function parse_url (str, component) {
// http://kevin.vanzonneveld.net
// +      original by: Steven Levithan (http://blog.stevenlevithan.com)
// + reimplemented by: Brett Zamir (http://brett-zamir.me)
// + input by: Lorenzo Pisani
// + input by: Tony
// + improved by: Brett Zamir (http://brett-zamir.me)
// %          note: Based on http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
// %          note: blog post at http://blog.stevenlevithan.com/archives/parseuri
// %          note: demo at http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
// %          note: Does not replace invalid characters with '_' as in PHP, nor does it return false with
// %          note: a seriously malformed URL.
// %          note: Besides function name, is essentially the same as parseUri as well as our allowing
// %          note: an extra slash after the scheme/protocol (to allow file:/// as in PHP)
// *     example 1: parse_url('http://username:password@hostname/path?arg=value#anchor');
// *     returns 1: {scheme: 'http', host: 'hostname', user: 'username', pass: 'password', path: '/path', query: 'arg=value', fragment: 'anchor'}
var key = ['source', 'scheme', 'authority', 'userInfo', 'user', 'pass', 'host', 'port', 
                    'relative', 'path', 'directory', 'file', 'query', 'fragment'],
    ini = (this.php_js && this.php_js.ini) || {},
    mode = (ini['phpjs.parse_url.mode'] && 
        ini['phpjs.parse_url.mode'].local_value) || 'php',
    parser = {
        php: /^(?:([^:\/?#]+):)?(?:\/\/()(?:(?:()(?:([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?()(?:(()(?:(?:[^?#\/]*\/)*)()(?:[^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
        strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
        loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // Added one optional slash to post-scheme to catch file:/// (should restrict this)
    };

var m = parser[mode].exec(str),
    uri = {},
    i = 14;
while (i--) {
    if (m[i]) {
      uri[key[i]] = m[i];  
    }
}

if (component) {
    return uri[component.replace('PHP_URL_', '').toLowerCase()];
}
if (mode !== 'php') {
    var name = (ini['phpjs.parse_url.queryKey'] && 
            ini['phpjs.parse_url.queryKey'].local_value) || 'queryKey';
    parser = /(?:^|&)([^&=]*)=?([^&]*)/g;
    uri[name] = {};
    uri[key[12]].replace(parser, function ($0, $1, $2) {
        if ($1) {uri[name][$1] = $2;}
    });
}
delete uri.source;
return uri;
 }
var URI = currentBrowser.currentURI.spec;

//unsure if this will work
var tab = document.getElementById("content").addTab("tools.whois.com.au/whois/?domain=" . parse_url("test.com", 'host'));
document.getElementById("content").selectedTab = tab;
document.getElementById("urlbar").focus();

  },

}

I am not used to javascript and this is definitely a Noob Question :)

Any help would be greatly appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给不了的爱 2024-11-10 08:05:27

甚至无需触及解析器,就会出现一个问题:

var tab = document.getElementById("content").addTab("tools.whois.com.au/whois/?domain=" . parse_url("test.com", 'host'));

javascript 不使用 . 作为加法运算符,它使用 +。您的控制台 (CTRL+SHIFT+J) 中可能存在错误,当您运行脚本时会告诉您这一点。

尝试:

var tab = document.getElementById("content").addTab("tools.whois.com.au/whois/?domain=" + parse_url("test.com", 'host'));

假设 parse_url() 函数传递正确的字符串。您可以使用以下方法进行测试:

window.alert(parse_url("test.com", 'host'))

Without even touching the parser, here's a problem:

var tab = document.getElementById("content").addTab("tools.whois.com.au/whois/?domain=" . parse_url("test.com", 'host'));

javascript doesn't use . as an addition operator, it uses +. Probably there is an error in your console (CTRL+SHIFT+J) that will tell you this when you run the script.

Try:

var tab = document.getElementById("content").addTab("tools.whois.com.au/whois/?domain=" + parse_url("test.com", 'host'));

This is assuming the parse_url() function passes the proper string. You can test that with:

window.alert(parse_url("test.com", 'host'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文