Removing Chrome's “translate” DOM Property

前端 未结 2 1228
轻奢々
轻奢々 2021-01-23 20:53

I\'m working with some legacy code where the original developers made heavy use of generating HTML DOM nodes with a non-standard attribute named translate

2条回答
  •  醉梦人生
    2021-01-23 21:50

    The best I've been able to come up with so far is going through every DOM element in the page defining a getter that checks for the existence of an attribute. (the Object.__defineGetter__ guard clause ensures no errors in browsers that don't support modern Javascript)

    if(Object.__defineGetter__)
    {
        var hasTranslateAttribute = function(){
            return $(this).hasAttribute("translate");
        };
        document.observe("dom:loaded", function() {
            $$('*').each(function(theElement){
                 theElement.__defineGetter__("translate", hasTranslateAttribute);
            });
        });
    }
    

    I tried defining a getting on Object.prototype and Element.prototype, but it seems like the browser's native translate is defined higher up the chain, so you need to redefine things on a per element basis.

提交回复
热议问题