简体   繁体   English

如何为此书签创建JavaScript对象?

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

I am trying to reverse engineer a bookmarklet that uses CasperJS. 我试图对使用CasperJS的小书签进行反向工程。

It creates an object called __utils__ that I can execute console commands with. 它创建了一个名为__utils__的对象,可以用来执行控制台命令。

The link to the bookmarklet is here:- 小书签的链接在这里:

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

Which references this JavaScript file:- 哪个引用了此JavaScript文件:

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

I have searched through the whole source code and I cannot find a reference to how this object is created. 我已经搜索了整个源代码,但是找不到关于如何创建该对象的参考。

Any pointers would be appreciated. 任何指针将不胜感激。

Look at the source of api.html . 查看api.html的源api.html After Just drag this link look at the JS in the href attribute. Just drag this link ,然后在href属性中查看JS。 Near the end it contains: 在结尾处包含:

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

The bookmarklet simply runs a small snippet of JavaScript code that appends a link to the clientutils.js to the document's end. 书签小程序仅运行一小段JavaScript代码,该代码将指向clientutils.js的链接附加到文档的末尾。 After that, it will run an anonymous function every 50 milliseconds that checks if the script has loaded (and has made the ClientUtils function available), and if it has, it stops running the function and creates window.__utils__ , thus making it available in the console. 之后,它将每隔50毫秒运行一个匿名函数,以检查脚本是否已加载(并使ClientUtils函数可用),如果已加载,它将停止运行该函数并创建window.__utils__ ,从而使该window.__utils__可用于控制台。 Here's the actual bookmarklet code in a more readable format. 这是更具可读性的实际小书签代码。 It should be pretty straightforward to understand: 它应该很容易理解:

(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