如果 result.responseText.match 不存在

发布于 2024-12-28 14:14:04 字数 984 浏览 0 评论 0原文

我对javascript不太了解,但我发现了我正在尝试编辑的greasemonekey脚本。

var rating = document.links;

for (i = 0; i < rating.length; i++) { 
if (rating[i].href.indexOf("/shows/") != -1){

    GM_xmlhttpRequest({
        method: 'get',
        url: rating[i].href,
        onload: function (i) {return function(result) { 

                rate = result.responseText.match(/<span class="rating">(.*)<\/span>/);
                result = rate[1].substring(0,3);

                rat = document.createElement("div");
                rat.className = 'rate';
                rat.innerHTML = result;
                rating[i].parentNode.insertBefore(rat, rating[i].nextSibling);
        }}(i)
    });
}
}

因此它会搜索页面上的所有链接,如果链接包含 /shows/ 它会搜索值:

<span class="rating"><\span>

如果找到它会在我创建的 div 中显示结果。到目前为止,一切都很好!

但其中一些链接确实有 <\span>

在这些链接上我喜欢说“未找到”但我不知道如何到 :(

i don't know mutch about javascript but i found this greasemonekey script that i'm trying to edit.

var rating = document.links;

for (i = 0; i < rating.length; i++) { 
if (rating[i].href.indexOf("/shows/") != -1){

    GM_xmlhttpRequest({
        method: 'get',
        url: rating[i].href,
        onload: function (i) {return function(result) { 

                rate = result.responseText.match(/<span class="rating">(.*)<\/span>/);
                result = rate[1].substring(0,3);

                rat = document.createElement("div");
                rat.className = 'rate';
                rat.innerHTML = result;
                rating[i].parentNode.insertBefore(rat, rating[i].nextSibling);
        }}(i)
    });
}
}

So it search all the links on the page and if the link contains /shows/ it search for the value:

<span class="rating"><\span>

if its found it shows the result in the div i created. so far so good!

But some of those links doenst have <span class="rating"><\span>

On those links i like it to say "not found" But i can't figured it out how to :(

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

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

发布评论

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

评论(1

赏烟花じ飞满天 2025-01-04 14:14:04
rate = result.responseText.match(/<span class="rating">(.*)<\/span>/);
if(rate){
  // Existing code.
  result = rate[1].substring(0,3);

  rat = document.createElement("div");
  rat.className = 'rate';
  rat.innerHTML = result;
  rating[i].parentNode.insertBefore(rat, rating[i].nextSibling);
}else{
  // Your new "not found" code.
  // Something like this, depending upon what you want to do:

  rat = document.createElement("div");
  rat.className = 'rate-not-found';
  rat.innerHTML = "Rate not found.";
  rating[i].parentNode.insertBefore(rat, rating[i].nextSibling);
}
rate = result.responseText.match(/<span class="rating">(.*)<\/span>/);
if(rate){
  // Existing code.
  result = rate[1].substring(0,3);

  rat = document.createElement("div");
  rat.className = 'rate';
  rat.innerHTML = result;
  rating[i].parentNode.insertBefore(rat, rating[i].nextSibling);
}else{
  // Your new "not found" code.
  // Something like this, depending upon what you want to do:

  rat = document.createElement("div");
  rat.className = 'rate-not-found';
  rat.innerHTML = "Rate not found.";
  rating[i].parentNode.insertBefore(rat, rating[i].nextSibling);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文