油猴插件(Tampermonkey)写一个简单的插件,修改初始化金币

302 阅读1分钟

这个游戏是开源的,可以修改,这篇文章只是为了讨论,学习之途

  1. 简介(普通用户请不要乱下载)

工具:油猴浏览器插件(www.tampermonkey.net/index.php?b…

游戏:塔防游戏(www.jq22.com/yanshi1757) image.png

初始化的金币只有500, 根本撑不过几关,所以打算更改一下初始化金币(成本最低)

  1. 代码分析

iframe嵌套游戏界面 image.png

游戏界面是canvas 画的 image.png

游戏规则逻辑写在_TD对象中, 在_TD中初始化方法_TD.init()运行完后直接删掉了 image.png

初始化金币的代码在

image.png

  1. 思路

3.1 本来打算使用 unsafeWindow 应用接口修改参数, 发现_TD对象中的方法都被删除的差不多了。而且我不知道怎么拿canvas中的变量对象,所以就放弃了。

3.2 重新加载修改过的js代码(我现在用的)

image.png

  1. 操作
// ==UserScript==
// @name         加金币
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.jq22.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=jq22.com
// @grant        unsafeWindow
// @grant        GM_xmlhttpRequest

// ==/UserScript==

// https://www.jq22.com/yanshi1757

(function() {
    'use strict';

    // Your code here..
    GM_xmlhttpRequest({
    method: "GET",
    url: "https://www.jq22.com/demo/build-150119213605/td-pkg-min.js?fmts=1293962401.7", // 替换为要拦截的JS文件的URL
    onload: function (response) {
      var modifiedCode = response.responseText.replace(
        "money:500",
        "money: 80000"
      );

      var indirectEval = eval;
      var modifiedFunction = indirectEval(modifiedCode);
      console.log('modifiedFunction', modifiedFunction);

      // clear canvas
      try {
        var iframe = document.getElementById("iframe");
        var iframeDocument =
          iframe.contentDocument || iframe.contentWindow.document;
        const canvas = iframeDocument.getElementById("td-canvas");
        canvas.remove();
        // add canvas
        const canvas2 = document.createElement("canvas");
        canvas2.id = "td-canvas";
        canvas2.width = 640;
        canvas2.height = 640;
        const parent = iframeDocument.getElementById("td-board");
        parent.appendChild(canvas2);
      } catch (error) {}

      // init
      _TD.init("td-board", true);
    },
  });

})();

碰到的问题: a. 这行代码总是报错影响后面的代码,但是又能运行,所以加了trycatch

var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;

b. 清空canvas, content.clearRect(0, 0, canvas.width, canvas.height);不管用,所以使用remove的方法

  1. 其他的方法

可以使用Burp Suite 拦截修改js

  1. 成绩单

还是打不过

image.png