Client-Side Dynamic Removal of [removed] Tags in <head>

后端 未结 5 2082
南笙
南笙 2021-01-31 10:37

Is it possible to remove script tags in the of an HTML document client-side and prior to execution of those tags?

On the server-side I am able

5条回答
  •  南方客
    南方客 (楼主)
    2021-01-31 10:57

    You could try to use the DOM Mutation events:

    DOMAttrModified
    DOMAttributeNameChanged
    DOMCharacterDataModified
    DOMElementNameChanged
    DOMNodeInserted
    DOMNodeInsertedIntoDocument
    DOMNodeRemoved
    DOMNodeRemovedFromDocument
    DOMSubtreeModified
    

    like so:

    document.head.addEventListener ('DOMNodeInserted', function(ev) {
       if (ev.target.tagName == 'SCRIPT') {
           ev.target.parentNode.removeChild(ev.target);
       }
    }, false);
    

    Also you can try the new way of doing this through MutationObserver

提交回复
热议问题