Is there a way to access JSON-LD via JavaScript if it doesn't have an ID?

前端 未结 2 1338
执笔经年
执笔经年 2020-12-19 07:59

I am trying to access the content in eventbrite emails that I receive but the html code doesn\'t have an ID associated to the JSON-LD script. So is there a way to still acce

相关标签:
2条回答
  • 2020-12-19 08:09
    function myFunction() {
        var todayDate = new Date();
        var label = GmailApp.getUserLabelByName("eventbrite");
        var threads = label.getThreads();
    
    
        for  (var i = 0; i < threads.length; i++) {
            threads[i].getMessages()[i].getBody().forEach(
                var jsonld = JSON.parse(document.querySelector('script[type="application/ld+json"]').innerText);
                document.getElementById('result').innerText = jsonld.endDate;
    
          if (jsonld.endDate > todayDate){
            threads[i].markUnread();
          } 
        )
      }
    }
    
    0 讨论(0)
  • 2020-12-19 08:29

    You can get all JSON-LD blocks with

     document.querySelectorAll('script[type="application/ld+json"]');
    

    or just the first one with

    document.querySelector('script[type="application/ld+json"]');
    

    Here's a full example:

    var jsonld = JSON.parse(document.querySelector('script[type="application/ld+json"]').innerText);
    document.getElementById('result').innerText = jsonld.endDate;
    <html>
      <head>
        <script type="application/ld+json">
          {
            "@context": "http://schema.org",
            "@type": "Event",
            "name": "A random event",
            "startDate": "2013-09-14T21:30",
            "endDate": "2013-09-14T21:30"
          }
        </script>
      </head>
      <body>
        <p>The end date is: <strong id="result"></strong></p>
      </body>
    </html>

    0 讨论(0)
提交回复
热议问题