小書籤只是運行的JavaScript代碼的小片段,附加的鏈接clientutils.js到文檔的末尾。之後,它將每隔50毫秒運行一次匿名函數,檢查腳本是否已加載(並已使ClientUtils
函數可用),如果已啓用,則停止運行該函數並創建window.__utils__
,從而使其在控制檯中可用。下面是一個更具可讀性格式的實際書籤代碼。它應該是相當簡單的理解:
(function() {
void(function() {
if (!document.getElementById('CasperUtils')) {
var CasperUtils = document.createElement('script');
CasperUtils.id = 'CasperUtils';
CasperUtils.src = 'https://raw.github.com/n1k0/casperjs/master/modules/clientutils.js';
document.documentElement.appendChild(CasperUtils);
var interval = setInterval(function() {
if (typeof ClientUtils === 'function') {
window.__utils__ = new window.ClientUtils();
clearInterval(interval);
}
}, 50);
}
}());
})();
感謝您的指導下,我忽略了書籤功能的第二部分。 – Lush 2013-02-26 01:28:17