javascript 书签在 '()' 上给出语法错误

发布于 2024-10-03 01:31:58 字数 1870 浏览 5 评论 0原文

我正在尝试制作一个小书签,它将加载我所在页面的黑客新闻讨论(如果存在)。

这是代码,因为我让它在 node.js 的 REPL 中运行:

// node.js stuff
require.paths.unshift('.');
var sys = require('util');
var alert = sys.puts;
var XMLHttpRequest = require("XMLHttpRequest").XMLHttpRequest;

//javascript: (function () {
    //url = 'http://api.ihackernews.com/getid?url=' + encodeURIComponent(window.location.href);
    url = 'http://api.ihackernews.com/getid?url=' + encodeURIComponent('http://blog.asmartbear.com/self-doubt-fraud.html');
    http = new XMLHttpRequest();
    http.open("GET", url, true);

    http.onreadystatechange = (function () {
        if (this.readyState == 4) {
            alert('foo');
            var ids = eval('(' + this.responseText + ')');
            if (ids.length > 0) {
                ids.reverse();
                //window.href = ids[0];
                alert(ids[0]);
            } else {
                alert('No stories found.');
            }
        }
    });

    http.send();
//})();

这按预期工作。 (它利用一个小文件来模拟节点中的XMLHttpRequest。)

取消注释函数定义行(并删除其他 Node-js 内容)给了我一个很好的小单行,一旦 打包

javascript:(function(){url='http://api.ihackernews.com/getid?url='+encodeURIComponent(window.location.href);http=new XMLHttpRequest();http.open("GET",url,true);http.onreadystatechange=(function(){if(this.readyState==4){alert('foo');var ids=eval('('+this.responseText+')');if(ids.length>0){window.href=ids[0]}else{alert('No stories found.')}}});http.send()})();

运行然而,它会提示 Firefox 的错误控制台向我提供非常有用的消息“语法错误”,后面跟着一个“()”,这是一个指向第二个括号后面的错误。

我没有使用 Firebug,因为它是 nightly 的,而 Firefox 的 nightly 目前不想合作。

我可能很快就会找到这个问题的解决方案(通常我是通过解释此文本框中的所有内容的过程来解决的),但我想我会很感激对此的任何帮助。这真的很困扰我。 :/

I am attempting to make a bookmarklet that will load up the Hacker News discussion for the page I'm on, if it exists.

Here's the code, as I have it to run in node.js's REPL:

// node.js stuff
require.paths.unshift('.');
var sys = require('util');
var alert = sys.puts;
var XMLHttpRequest = require("XMLHttpRequest").XMLHttpRequest;

//javascript: (function () {
    //url = 'http://api.ihackernews.com/getid?url=' + encodeURIComponent(window.location.href);
    url = 'http://api.ihackernews.com/getid?url=' + encodeURIComponent('http://blog.asmartbear.com/self-doubt-fraud.html');
    http = new XMLHttpRequest();
    http.open("GET", url, true);

    http.onreadystatechange = (function () {
        if (this.readyState == 4) {
            alert('foo');
            var ids = eval('(' + this.responseText + ')');
            if (ids.length > 0) {
                ids.reverse();
                //window.href = ids[0];
                alert(ids[0]);
            } else {
                alert('No stories found.');
            }
        }
    });

    http.send();
//})();

This works as expected. (It makes use of a little file to simulate XMLHttpRequest in node.)

Uncommenting the function definition lines (and removing the other node-js stuff) gives me a nice little one-liner, once packed:

javascript:(function(){url='http://api.ihackernews.com/getid?url='+encodeURIComponent(window.location.href);http=new XMLHttpRequest();http.open("GET",url,true);http.onreadystatechange=(function(){if(this.readyState==4){alert('foo');var ids=eval('('+this.responseText+')');if(ids.length>0){window.href=ids[0]}else{alert('No stories found.')}}});http.send()})();

Running it, however, prompts Firefox's error console to give me the incredibly helpful message "syntax error", followed by a "()" an an error pointing to right after the second parenthesis.

I'm not using Firebug because its nightly and Firefox's nightly don't want to cooperate at the moment.

The solution to this will probably come to me soon (normally I figure it out from the process of explaining everything in this text box), but I suppose I'd appreciate any help on this. It's really bothering me. :/

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

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

发布评论

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

评论(1

薄荷港 2024-10-10 01:31:58

这是因为您的响应为空(由于 同源策略)基本上是在执行此操作:

eval('()'); //SyntaxError: Unexpected token )

您需要添加检查是否有响应,如下所示:

http.onreadystatechange = (function () {
    if (this.readyState == 4) {
        if(this.responseText) { //was the response empty?
          var ids = eval('(' + this.responseText + ')');
          if (ids.length > 0) {
            ids.reverse();
            window.href = ids[0];
          }
        } else {
            alert('No stories found.');
        }
    }
});

It's because your response being blank (due to the same origin policy) is basically executing this:

eval('()'); //SyntaxError: Unexpected token )

You need to add a check if there's a response at all, like this:

http.onreadystatechange = (function () {
    if (this.readyState == 4) {
        if(this.responseText) { //was the response empty?
          var ids = eval('(' + this.responseText + ')');
          if (ids.length > 0) {
            ids.reverse();
            window.href = ids[0];
          }
        } else {
            alert('No stories found.');
        }
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文