generated from LiteLoaderQQNT/Plugin-Template
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Escaped removed dirty HTML instead of removal.
When dirty node found inside HTML, escaped to show the innerHTML of the element as pure text instead of just remove it sliently.
- Loading branch information
Showing
4 changed files
with
57 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Utils function about HTML string process | ||
|
||
import { mditLogger } from "./logger"; | ||
|
||
const DOMPurify = require('dompurify'); | ||
|
||
DOMPurify.addHook('uponSanitizeElement', function (node: HTMLElement, data: any) { | ||
// mditLogger('debug', 'PurifyHook', 'Data', data); | ||
if (data.allowedTags[data.tagName] === true) { | ||
// mditLogger('debug', 'PurifyHook', 'Hook skipped'); | ||
return; | ||
} | ||
let newNode = document.createElement('p'); | ||
newNode.innerText = node.outerHTML; | ||
// mditLogger('debug', 'PurifyHook', 'New node', newNode); | ||
node.replaceWith(newNode); | ||
}); | ||
|
||
interface UponSanitizeDataRecv { | ||
tagName: string; | ||
allowedTags: Record<string, boolean>; | ||
} | ||
|
||
/** | ||
* Unescape HTML entities in HTML string. Already unescaped HTML tag string will be ignored and not shown | ||
* in return string. | ||
* @param {string} input | ||
* @returns {string} String with all HTML entities unescaped | ||
*/ | ||
export function unescapeHtml(input: string) { | ||
var doc = new DOMParser().parseFromString(input, "text/html"); | ||
return doc.documentElement.textContent; | ||
} | ||
|
||
export function escapeHtml(input: string) { | ||
return input | ||
.replaceAll('&', '&') | ||
.replaceAll('<', '<') | ||
.replaceAll('>', '>') | ||
.replaceAll('"', '"') | ||
.replaceAll("'", '''); | ||
} | ||
|
||
/** | ||
* Using DOMPurify to purify HTML | ||
* @param {string} input | ||
* @return {string} Purified HTML string. | ||
*/ | ||
export function purifyHtml(input: string) { | ||
let res = DOMPurify.sanitize(input); | ||
mditLogger('debug', 'Purify', 'Removed', DOMPurify.removed); | ||
return res; | ||
} |