Removing Chrome's “translate” DOM Property

前端 未结 2 1224
轻奢々
轻奢々 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:42

    Replace the nonstandard attribute translate by an attribute like data-translate, which is virtually guaranteed to be and to remain undefined in HTML specifications and in browsers. The data-* attributes were invented to prevent issues like this, and they can also be used to fix them.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题