簡體   English   中英

如何為此書簽創建JavaScript對象?

[英]How is this JavaScript object created for this bookmarklet?

我試圖對使用CasperJS的小書簽進行反向工程。

它創建了一個名為__utils__的對象,可以用來執行控制台命令。

小書簽的鏈接在這里:

http://casperjs.org/api.html#bookmarklet

哪個引用了此JavaScript文件:

https://raw.github.com/n1k0/casperjs/master/modules/clientutils.js

我已經搜索了整個源代碼,但是找不到關於如何創建該對象的參考。

任何指針將不勝感激。

查看api.html的源api.html Just drag this link ,然后在href屬性中查看JS。 在結尾處包含:

window.__utils__=new%20window.clientUtils();

書簽小程序僅運行一小段JavaScript代碼,該代碼將指向clientutils.js的鏈接附加到文檔的末尾。 之后,它將每隔50毫秒運行一個匿名函數,以檢查腳本是否已加載(並使ClientUtils函數可用),如果已加載,它將停止運行該函數並創建window.__utils__ ,從而使該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);
    }
  }());
})();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM