简体   繁体   English

如何保护本机函数不被Google API覆盖

[英]How to protect a native function from being overwritten by Google API

I have the following native JavaScript function on my web platform: 我的网络平台上具有以下本机JavaScript函数:

function tag_(t)
{
 return (document.getElementsByTagName(t)) ? document.getElementsByTagName(t) : false;
}

There is also the prototype: 还有一个原型:

Object.prototype.tag_ = function(t)
{
 return (this.getElementsByTagName(t)) ? this.getElementsByTagName(t) : false;
}

When this function works normally it returns an array of elements from the DOM. 当此函数正常工作时,它将从DOM返回一个元素数组。

While implementing Google Calendar's API I found that it decided to override this function (not the prototype though). 在实施Google Calendar的API时,我发现它决定覆盖此功能(尽管不是原型)。 While going through all of the raw JavaScript data loaded (with cache disabled as always) I found no mention of the string 'tag_'. 在浏览所有加载的原始JavaScript数据(一如既往地禁用缓存)时​​,我没有提到字符串'tag_'。

How do I protect a native function on my web platform from being overridden by a third party? 如何保护Web平台上的本机功能不被第三方覆盖?

Renaming the function and not using Google's Calendar API are not an acceptable answers. 重命名功能,而不是使用谷歌的日历API是不是一个可以接受的答案。


Apparently it is possible to "freeze" or "seal" objects: 显然可以“冻结”或“密封”对象:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze

This does not seem to work however: 但是,这似乎不起作用:

Object.freeze(tag_);

This question does not have a working answer. 这个问题没有一个工作的答案。

After some research I came across JavaScript proxy ( new Proxy(object_being_watched,function_called_onchange); ) however that does not work with functions, only objects. 经过一些研究,我遇到了JavaScript代理( new Proxy(object_being_watched,function_called_onchange); ),但是它不适用于函数,只能用于对象。 "There is no such thing as an array in JavaScript! There are only objects!" “ JavaScript中没有数组这样的东西!只有对象!” -- "Functions are not objects." -“功能不是对象。” If JavaScript were not designed to be fanatically flexible it would be much more useful . 如果JavaScript不被设计成具有狂热的灵活性,那将更加有用


Here is a hack work-around though it is very subjective to various conditions. 这是一种变通办法,尽管它会受各种条件的影响很大。

setTimeout(function()
{
 window['tag_'] = function(t)
 {
 return (document.getElementsByTagName(t)) ? document.getElementsByTagName(t) : false;
 }
},500);

This does not seem to work however: 但是,这似乎不起作用:

   Object.freeze(Object.prototype.tag_);

Because Object.prototype.tag_ is undefined and sealing that makes no sense. 因为Object.prototype.tag_undefined ,所以密封是没有意义的。 However you could: 但是,您可以:

  Object.freeze(window);

but that would be a bad idea. 但这是一个坏主意。 Instead just do proper scoping: 相反,只需进行适当的作用域设置即可:

  (function main() {
    function tag_(t) {
       return document.getElementsByTagName(t);
    }
    //...
  })();

也许在加载Google Calendar API之前先复制函数,然后在加载后将其还原?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM