Remove javascript event handlers from elements

This commit is contained in:
Jackson Harper
2024-08-27 14:35:38 +08:00
parent 1a2dbf6034
commit b2d36330c9

View File

@ -303,7 +303,7 @@ Readability.prototype = {
if (!this._keepClasses) { if (!this._keepClasses) {
// Remove classes. // Remove classes.
this._cleanClasses(articleContent); this._cleanElement(articleContent);
} }
}, },
@ -456,7 +456,7 @@ Readability.prototype = {
* @param Element * @param Element
* @return void * @return void
*/ */
_cleanClasses: function (node) { _cleanElement: function (node) {
if (node.className && node.className.startsWith && node.className.startsWith('_omnivore')) { if (node.className && node.className.startsWith && node.className.startsWith('_omnivore')) {
return; return;
} }
@ -483,8 +483,10 @@ Readability.prototype = {
node.removeAttribute("class"); node.removeAttribute("class");
} }
_removeAllEventHandlers(media)
for (node = node.firstElementChild; node; node = node.nextElementSibling) { for (node = node.firstElementChild; node; node = node.nextElementSibling) {
this._cleanClasses(node); this._cleanElement(node);
} }
}, },
@ -546,7 +548,6 @@ Readability.prototype = {
this._forEachNode(medias, function (media) { this._forEachNode(medias, function (media) {
var src = media.getAttribute("src"); var src = media.getAttribute("src");
var poster = media.getAttribute("poster"); var poster = media.getAttribute("poster");
var srcset = media.getAttribute("srcset");
if (src) { if (src) {
media.setAttribute("src", this.toAbsoluteURI(src)); media.setAttribute("src", this.toAbsoluteURI(src));
@ -558,6 +559,20 @@ Readability.prototype = {
}); });
}, },
// removes all the javascript event handlers from the supplied element
_removeAllEventHandlers(element) {
const attributes = element.attributes;
// Iterate in reverse because removing attributes changes the length
for (let i = attributes.length - 1; i >= 0; i--) {
const attribute = attributes[i];
// Check if the attribute starts with "on" (like "onload", "onerror", etc.)
if (attribute.name.startsWith('on')) {
element.removeAttribute(attribute.name);
}
}
},
/** Creates imageproxy links for all article images with href source */ /** Creates imageproxy links for all article images with href source */
_createImageProxyLinks: function (articleContent) { _createImageProxyLinks: function (articleContent) {
if (this.createImageProxyUrl !== undefined) { if (this.createImageProxyUrl !== undefined) {