DOMParser Inside Coda Packs

Hi all, I would like to create a pack that will show the amount of followers specific LinkedIn profile has by using DOMParser - Web APIs | MDN

Input:
https://www.linkedin.com/in/faridsabitov/

Output:
2090

There is no easy way to pull that from API and I thought that just parsing HTML could help. Here is the alert that I’m getting when I’m trying to build the pack

Thank you for your help!

Hi @Farid_Sabitov - This error is due to the fact that the Packs runtime only contains the APIs available in the core JavaScript specification (ECMAScript). This particular API, DOMParser, is only available in web browsers. You can read more about this limitation in the Using libraries guide in the documentation.

Luckily folks often make polyfills for various Web APIs to allow them to be used on the server. In this case it seem that the NPM module xmldom could provide the same functionality. However, as mentioned in that libraries guide not all NPM modules are compatible with the Packs runtime, as sometimes they rely on Node.js-specific APIs, which again aren’t present. It’s hard to tell if a library will work ahead of time, so it may be worth trying this one and see if it works.

1 Like

Hi @Eric_Koleda,

Thank you for a quick response! I’ve installed the the library and built it locally and unfortunately, I see another alert

And here are the “pack.ts” contents

// This import statement gives you access to all parts of the Coda Packs SDK.
import * as coda from "@codahq/packs-sdk";

// This line creates your new Pack.
export const pack = coda.newPack();

// Adding HTML Parser
import { DOMParser } from 'xmldom';

function parse(htmlInput) {
  
    const doc = new DOMParser().parseFromString(
      '<xml xmlns="a" xmlns:c="./lite">\n' +
      '\t<child>test</child>\n' +
      '\t<child></child>\n' +
      '\t<child/>\n' +
      '</xml>',
      'text/xml'
    )
    doc.documentElement.setAttribute('x', 'y')
    doc.documentElement.setAttributeNS('./lite', 'c:x', 'y2')
    console.info(doc)
  
    const nsAttr = doc.documentElement.getAttributeNS('./lite', 'x')
    console.info(nsAttr)
  
    return nsAttr    
  }

// Here, we add a new formula to this Pack.
pack.addFormula({
  // This is the name that will be called in the formula builder.
  // Remember, your formula name cannot have spaces in it.
  name: "GetFollowers",
  description: "Show an amount of followers for the user",

  // If your formula requires one or more inputs, you’ll define them here.
  // Here, we're creating a string input called “name”.
  parameters: [
    coda.makeParameter({
      type: coda.ParameterType.String,
      name: "link",
      description: "Please provide a full link to the LinkedIn account. Ex.: https://www.linkedin.com/in/faridsabitov/",
    }),
  ],

  // The resultType defines what will be returned in your Coda doc. Here, we're
  // returning a simple text string.
  resultType: coda.ValueType.String,

  // Everything inside this execute statement will happen anytime your Coda
  // formula is called in a doc. An array of all user inputs is always the 1st
  // parameter.
  execute: async function ([name], context) {
    return parse(name);
  },
});

And the way how I run it in Terminal is:

npx coda execute pack.ts GetFollowers "https://www.linkedin.com/in/faridsabitov/"

Unfortunately, it’s not working. I hope somebody will create a web parser pack for Coda on Coda Hackathon. @Eric_Koleda, please let me know if I’m making a mistake here, thank you!

Your code looks reasonable, so it could be that this library isn’t compatible with the Packs runtime for some reason. In most cases there are usually a few different libraries to select from to accomplish the same task, so you might have luck with an alternative one. For example, I know that the HTML Pack was built using the cheerio library.

1 Like