How to parse an RSS feed using JavaScript in QuarkXPress (JQuery example)

Discuss scripting using AppleScript in QuarkXPress 2015, 2016, 2017 & 2018.
Discuss scripting using JavaScript in QuarkXPress 2018.
Post Reply
User avatar
Sarbjit Singh
Quarkian
Quarkian
Posts: 410
Joined: 22 Jan 2009, 04:44
Location: India
Contact:

How to parse an RSS feed using JavaScript in QuarkXPress (JQuery example)

Post by Sarbjit Singh » 23 Jul 2018, 01:47

This is our first example that uses the JQuery library to read an RSS feed. It logs the results in the JavaScript Debugger Palette's console.

The intent is to show how you can use any web resource using native JavaScript, or a third party JS library. You may extend this example further to populate text boxes on a layout, with content fetched from the RSS feed. We are using the Quark Forum RSS in this example. However, you may use any RSS URL and change the appropriate tags that you need to read from the feed.

Feel free to ask questions.

Here is the code example with detailed comments:

You may download the snippet here:
https://pastebin.com/3bsx3Snq
OR here:
https://github.com/qxpjs/examples

//Import jQuery Library from the CDN, you don't even need to download it!
app.importScript("https://code.jquery.com/jquery-3.3.1.js");

(function () {

//Specify the RSS Feed Link
const rssFeed = "http://forums.quark.com/feed.php";
//Check the feed to know which Tag is used for each entry, specify the correct Tag here
const rssPostTag = "entry";
//Create an array of Article Tags you want to fetch from the feed
const keysToRead = ["title", "author", "id"];

//Call the function to read the feed
readRSS();

//*****************====================================Functions used in the JavaScript===============================****************//

function readRSS() {
let articleList = []; //Create an array to store articles
let article; //variable to store each article
let promise = new Promise(function (resolve, reject)//promise is used to ensure this task completes and returns a promise followed by further execution
{
//Load the RSS Feed XML using JQuery's "get" method
$.get(rssFeed, function (data) {
//Parse all the "entries" in XML
$(data).find(rssPostTag).each(function () {
var el = $(this);
//Create an object to store articles
article = new Object();
//Loop through all the required Tags and store them in the object
for (let i = 0; i < keysToRead.length; i++) {
article[keysToRead] = el.find(keysToRead).text();
}

//Push to the list
articleList.push(article);
//Print the Element on the JavaScript Debugger console
console.log(article);
});

console.log("Found " + articleList.length + " articles in the feed.");
});

// resolve(articleList);
Promise.resolve().then(resolve(articleList));
});
return promise;
}

})();
Last edited by Sarbjit Singh on 23 Jul 2018, 05:15, edited 1 time in total.
Image

User avatar
Matthias Guenther (Quark)
Quarkian
Quarkian
Posts: 2467
Joined: 04 Jun 2004, 15:06
Location: http://bit.ly/QuarkHamburg
Contact:

Re: How to parse an RSS feed using JavaScript in QuarkXPress (JQuery example)

Post by Matthias Guenther (Quark) » 23 Jul 2018, 04:42

Great script, Sarbjit.

And congratulations to your 400th posts, despite Facebook taking over ;-)

Regards
Matthias
Feel there should be more chatting, more tips, more user interaction?
Join us on Facebook, a forum-like group with over 2,000 QuarkXPress fans interacting:
http://www.facebook.com/groups/quarkxpress

Image

Post Reply

Return to “QuarkXPress 20xx: Scripting (AppleScript / JavaScript)”