【问题标题】:Browser Native Injection浏览器原生注入
【发布时间】:2015-06-28 17:16:34
【问题描述】:

以下脚本:#content_script.js,这是一种注入代码以获取参数中传递的值的方法。

或者更清楚(可以是任何 JavaScript 的函数):

(function() {
    var parse = JSON.parse;
    JSON.parse = function(){
        console.log('Getting params: ', arguments);
        return parse.apply(JSON, arguments)
    };
    JSON.parse.toString = function(){ return 'function parse() { [native code] }' };

    var wss = WebSocket.prototype.send;
    WebSocket.prototype.send = function(){
        console.log('Getting params', arguments);
        return wss.apply(this, arguments)
    }
    WebSocket.prototype.send.toString = function(){ return 'function send() { [native code] }' }
})();

但是,在一个在线游戏中,我不应该使用该方法,而是想将其注入 JavaScript 引擎 (Native Code)。不完全是我想知道如何开发,如果没有,我该怎么办?。如果我必须使用另一种编程语言或某种方法来做到这一点?

【问题讨论】:

    标签: javascript c++ google-chrome google-chrome-extension firefox-addon


    【解决方案1】:

    这很容易做到。下面的所有代码都是模板代码,这意味着所有插件的通用复制粘贴,略有调整。这是一个关于如何编写 firefox bootstrap 插件的小教程。它与此处基本相同,但为您的工作个性化:https://gist.github.com/Noitidart/9025999(虽然我没有包括图标和本地化等细节)

    1. 在您的计算机上创建一个空文件夹
    2. 在其中创建这些空白的新文件:bootstrap.js 和 install.rdf 以及 chrome.manifest 和 inject.js
    3. 在 install.rdf 中粘贴这个模板:

      <?xml version="1.0" encoding="utf-8"?>
          <!-- This Source Code Form is subject to the terms of the Mozilla Public
         - License, v. 2.0. If a copy of the MPL was not distributed with this
         - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
          <RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:em="http://www.mozilla.org/2004/em-rdf#">
            <Description about="urn:mozilla:install-manifest">
              <em:id>Bootstrap-Skeleton@jetpack</em:id>
              <em:version>initial</em:version>
              <em:type>2</em:type>
              <em:bootstrap>true</em:bootstrap>
              <em:unpack>false</em:unpack>
      
              <!-- Firefox -->
              <em:targetApplication>
                <Description>
                  <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
                  <em:minVersion>7.0</em:minVersion>
                  <em:maxVersion>27.0</em:maxVersion>
                </Description>
              </em:targetApplication>
      
              <!-- Front End MetaData -->
              <em:name>Bootstrap Skeleton</em:name>
              <em:description>How all bootstrap addons start.</em:description>
              <em:creator>Noitidart</em:creator>
              <em:contributor>Pat for Icon</em:contributor>
              <em:optionsType>2</em:optionsType>
            </Description>
          </RDF>
      
    4. 现在在我们粘贴的内容中,让我们将&lt;em:id&gt; 的内容替换为DragonboundCheater@wZVanG

    5. 让我们将&lt;em:name&gt; 更新为我们想要的插件名称,我们将其命名为Dragonbound Cheater
    6. 现在保存并转到 bootstrap.js
    7. 在 bootstrap.js 中粘贴这个模板:

      function startup(aData, aReason) {}
      
      function shutdown(aData, aReason) {
          if (aReason == APP_SHUTDOWN) return;
      }
      
      function install() {}
      
      function uninstall() {}
      
    8. 现在用这个更新它的内容:

      const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
      Cu.import('resource://gre/modules/Services.jsm');
      var browserWinAddedTo;
      function startup(aData, aReason) {
          var recentBrowserWindow = Services.wm.getMostRecentWindow('navigator:browser');
          browserWinAddedTo = recentBrowserWindow;
          if (recentBrowserWindow.document.readyState == 'complete') { //on startup `aDOMWindow.document.readyState` is `uninitialized`
              recentBrowserWindow.messageManager.loadFrameScript('chrome://dragonboundcheater@wzvang/content/inject.js');
          } else {
              recentBrowserWindow.addEventListener('load', function () {
                  recentBrowserWindow.removeEventListener('load', arguments.callee, false);
                  recentBrowserWindow.messageManager.loadFrameScript('chrome://dragonboundcheater@wzvang/content/inject.js');
              }, false);
          }
      }
      
      function shutdown(aData, aReason) {
          if (aReason == APP_SHUTDOWN) return;
          browserWinAddedTo.messageManager.removeDelayedFrameScript('chrome://dragonboundcheater@wzvang/content/inject.js');
      }
      
      function install() {}
      
      function uninstall() {}
      
    9. 在 chrome.manifest 中添加:content dragonboundcheater ./

    10. 现在在 inject.js 中我们可以做任何 js 想做的事情,但首先让我们确保主机匹配,并看到 https://developer.mozilla.org/en-US/Firefox/Multiprocess_Firefox/Frame_script_environment 这告诉我们窗口对象是由全局变量 content 引用的,所以无论我们在哪里想要访问我们使用content,如果你想访问我们通过content.wrappedJSObject 访问的js 环境。所以让我们把 inject.js 变成这样:

      (function() {
          var window = content;
          var js = window.wrappedJSObject;
      
      
          if (window.location.host.indexOf('dragonbound.net') == -1) {
             return;
          }
      
          var parse = js.JSON.parse;
          js.JSON.parse = function(){
              console.log('Getting params: ', arguments);
              return parse.apply(js.JSON, arguments)
          };
          js.JSON.parse.toString = function(){ return 'function parse() { [native code] }' };
      
          var wss = js.WebSocket.prototype.send;
          js.WebSocket.prototype.send = function(){
              console.log('Getting params', arguments);
              return wss.apply(this, arguments)
          }
          js.WebSocket.prototype.send.toString = function(){ return 'function send() { [native code] }' }
      })();
      
    11. 然后将文件夹中的所有内容压缩起来,将其从 .zip 重命名为 .xpi 并拖入 firefox,瞧 :)

    【讨论】:

    • 注意我有一个错误:在新打开的选项卡上它没有运行脚本,我必须调查一下,稍后会做
    • 安装成功。但我得到了这个控制台的线路:No chrome package registered for chrome://wzvang/content/inject.js
    • 谢谢,我已经明白了,但是我必须阅读文档是如何工作的。而且我希望页面不以任何方式检测 JavaScript 的处理。你有什么建议吗?
    • @wZVanG 我不明白你的问题,你是什么意思没有检测到 js 处理?
    • 我的意思是如果页面,在这种情况下dragonbound.net 可能有一些方法来检测或知道我的注入?这是我不想发生的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多