【问题标题】:javascript running befor jquery in chrome extension [closed]在 chrome 扩展中运行 jquery 之前的 javascript [关闭]
【发布时间】:2014-09-16 00:09:36
【问题描述】:

我正在尝试更改使用 chrome 扩展名下载的文件的名称。我所做的是解析网页(使用 jQuery),从中给出文件的名称。现在这是我的代码结构:

download event listener{
javascript code
jquery code to parse webpage
change file name 
}

发生的情况是文件在 jquery 完成解析和更改文件名之前下载,代码在 jquery 完成执行之前运行,这反过来导致错误的文件名。我不明白问题是什么以及如何纠正它。我认为不需要完整的代码,所以我不发布它。有什么方法可以限制文件名更改,直到 jquery 完成? 编辑 代码:

chrome.downloads.onDeterminingFilename.addListener( 
        function (downloadItem, suggest) 
        {   window.alert(downloadItem.url);
            window.alert("inside downloads");
            if (/^https?:\/\/ieeexplore\.ieee\.org.*/.test(downloadItem.referrer)) 
            {   
                x=downloadItem.url;
                window.alert("match done")
                folder="newww";
                window.alert(String(downloadItem.referrer));
                z=downloadItem.referrer;
                var res = z.split("arnumber=");
                var res1 = res[1].split("&");
                alert(res1[0]);
                console.log(res1[0]);
                u="http://ieeexplore.ieee.org/xpl/articleDetails.jsp?tp=&arnumber="+res1[0];
                console.log(u);
                $(document).ready(function(){
                $.get(u, function(data) 
                {//parse webpage here
                 //set the value of name here
                });
                 });
            if(isPDF(downloadItem))
            {   alert("lame");
                suggest({filename: folder + "/" + name});
            }
            else suggest();
            }


        });

    function isPDF(item)
    {
      if(item.mime === "application/pdf") return true;
      else if (item.filename.match(/\.pdf$/i)) return true;
      else return false;
    }

问题是更改名称的 if 函数在 jquery 之前运行...

【问题讨论】:

  • 也许有人能说出投票反对的原因!!
  • 我很乐意。您应该阅读How to Ask 指南并相应地编辑您的问题。而且您确实应该包含相关代码。
  • 我会写评论,但不幸的是我没有足够的声誉来这样做。反正。首先,我认为您被否决了,因为您的问题非常不清楚。您的描述非常模糊,您没有足够详细地解释您实际上在做什么以及如何做。可能发布代码会有所帮助。对于您的实际问题。您可以尝试按照其他答案中的建议将回调函数传递给您的下载函数和/或查看 $(document).ready。或者更简单:你为什么不简单地改变订单?解析网页,下载文件,重命名。还是你的意思是 t
  • @Xan 请看代码..
  • 我的反对意见仍然存在,这仍然是一个可怕的问题。

标签: javascript jquery google-chrome google-chrome-extension


【解决方案1】:

大家好消息,你们can call suggest asynchronously

if (/^https?:\/\/ieeexplore\.ieee\.org.*/.test(downloadItem.referrer)) 
{   
  /* ... */
  $.get(u, function(data) 
  {
    //parse webpage here
    //set the value of name here
    suggest({filename: result}); // Called asynchronously
  });
  return true; // Indicate that we will call suggest() later
}

这里的关键点:chrome.downloads.onDeterminingFilename 处理程序将在您的$.get 回调被执行之前退出。默认情况下,Chrome 在这种情况下不会等待您的建议 - 但您可以通过返回 true 来表示“我稍后会告诉您,请稍候”。这些都写在文档里了。

请阅读一些关于异步函数的教程。例如,this answer

【讨论】:

    【解决方案2】:

    我不完全确定你的问题是什么,但也许

    $(document).ready(function(){
        // some code that gets executed when the page finished loading
    });
    

    可能会对你有所帮助。

    如果您用于下载文件的函数有 callback,您可以查看 jQuery 文档。然后你可以做一些类似的事情

    somefiledownload.ready(function(){
        // some code that gets executed when the download finished
    });
    

    【讨论】:

    • 我认为他的问题可以描述为:你的概念somefiledownload.ready在数据收集之前被调用,它必须同步返回一个建议的文件名。
    • 我正在发布代码..请看一下..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    相关资源
    最近更新 更多