Skip to content

Commit

Permalink
feat: remove DOMParser to prevent memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Feb 23, 2024
1 parent 6ab939f commit 653f0db
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/lib/rsshub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import { parse } from "tldts"
import { parseRules } from "./rules"
import type { RSSData, Rule } from "./types"

const parseHtml = (html) => {
const template = document.createElement('template');
template.innerHTML = html;
return template.content;
}

function ruleHandler(rule: Rule, params, url, html, success, fail) {
const run = () => {
let resultWithParams
if (typeof rule.target === "function") {
try {
const parser = new DOMParser()
const document = parser.parseFromString(html, "text/html")
const document = parseHtml(html)
resultWithParams = rule.target(params, url, document)
} catch (error) {
resultWithParams = ""
Expand Down
2 changes: 1 addition & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export type Rule = {
title: string
docs: string
source: string[]
target: string | ((params: any, url: string, document: Document) => string)
target: string | ((params: any, url: string, document: DocumentFragment) => string)
}

export type Rules = {
Expand Down

0 comments on commit 653f0db

Please sign in to comment.