1

I am running some code in NodeJS to remove script tags before executing the scripts. Is there a simple way to do something similar in a browser environment?

import cheerio from "cheerio";

export async function removeScripts(serializedSvg: string): Promise<string> {
  const $ = cheerio.load(serializedSvg, {
    xmlMode: true,
    decodeEntities: false,
  });

  Array.from($("script")).forEach((el: CheerioElement) => {
    $(el).remove();
  });

  return $.xml();
}
3
  • afaik, cheerio is basically core jQuery for node. So just look up how to parse xml with jQuery and parts of your code can be used 1:1. Commented May 15, 2020 at 9:52
  • @ChrisG: Thank you, that's a good idea Commented May 15, 2020 at 11:23
  • Does this answer your question? Is there a better way to sanitize input with javascript? Commented May 15, 2020 at 11:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.