我已經寫了一個Firefox插件第一次,它在幾個月前被審查和接受。這個插件經常會調用第三方API。與此同時,它再次審查,現在它稱爲setInterval的方式被批評:Firefox插件中使用setInterval的潛在漏洞?
setInterval
調用潛在危險的方式。爲了防止漏洞,setTimeout和setInterval函數應該只用函數表達式作爲它們的第一個參數來調用。引用函數名稱的變量是可以接受的,但不推薦使用,因爲它們不適用於靜態源驗證。
這是關於我的插件的「體系結構」的一些背景知識。它使用一個全局對象是不超過一個命名空間更:
if ('undefined' == typeof myPlugin) {
var myPlugin = {
//settings
settings : {},
intervalID : null,
//called once on window.addEventlistener('load')
init : function() {
//load settings
//load remote data from cache (file)
},
//get the data from the API
getRemoteData : function() {
// XMLHttpRequest to the API
// retreve data (application/json)
// write it to a cache file
}
}
//start
window.addEventListener(
'load',
function load(event) {
window.removeEventListener('load', load, false); needed
myPlugin.init();
},
false
);
}
所以這可能不是最好的做法,但我一直在學習。間隔本身被稱爲init()
方法內,像這樣:
myPlugin.intervalID = window.setInterval(
myPlugin.getRemoteData,
myPlugin.settings.updateMinInterval * 1000 //milliseconds!
);
還有另一個點設置間隔:觀察者的設置(偏好)清除當前間隔,並將其設置完全相同的方式像上面當提到更改爲updateMinInterval設置。
當我得到this權,使用»函數表達式的解決方案«應該是這樣的:
myPlugin.intervalID = window.setInterval(
function() {
myPlugin.getRemoteData();
},
myPlugin.settings.updateMinInterval * 1000 //milliseconds!
);
我說得對不對?
什麼是「攻擊」此代碼的可能情況,至今我忽略了?
應該setInterval
和setTimeout
基本上以另一種方式在Firefox插件中使用,然後在»normal«前端javascripts?因爲setInterval的文檔正好顯示了在某些示例中使用聲明函數的方式。