11

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 access this data? If so, how?

Is it possible to perhaps attach a temporary id to the JSON-LD script tag so that I can access the data? If so, how?

Prachy Mohan
  • 131
  • 1
  • 1
  • 4
  • Welcome to Stack Overflow. [Read here](http://stackoverflow.com/help/mcve) for more information about how to create a Minimal, Complete and Verifiable question. – Toby Jul 27 '16 at 02:12
  • Why do you need an ID to access it? – unor Jul 27 '16 at 15:49
  • Unor, when I searched the internet the examples all showed using ID so wasn't sure how to access it without using ID. – Prachy Mohan Jul 29 '16 at 00:07

2 Answers2

33

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>
Markus Lanthaler
  • 3,683
  • 17
  • 20
2
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();
      } 
    )
  }
}
user487772
  • 8,800
  • 5
  • 47
  • 72
Prachy Mohan
  • 131
  • 1
  • 1
  • 4