首页 > 解决方案 > 使用 Greasemonkey 覆盖匿名函数

问题描述

我正在尝试使用 Greasemonkey 将此代码更改为不使用“世界”,而是使用 window.location.href 以便在选择下拉选项时,它保持在同一页面上,但只是更改了 suid。

页面代码:

<script type="text/javascript">
$('#charselectdropdown').on('select2:select', function (e) {
  var data = e.params.data;
  if(data.id > 0) {
    window.location = '/world?suid=' + data.id + '&serverid=2';
  }
  else {
    window.location = '/myaccount';
  }
});
</script>

我的尝试:

// ==UserScript==
// @name           The OW Helper
// @include        *.outwar.com/*
// @exclude        *.outwar.com/myaccount*
// @require        https://gist.github.com/raw/2620135/checkForBadJavascripts.js
// @require        http://code.jquery.com/jquery-latest.js
// @grant          GM.setClipboard
// @grant          GM_openInTab
// @grant          GM_registerMenuCommand
// @grant          GM_addStyle
// @run-at         document-start

// ==/UserScript==
(function (w) {
    function pre_script_execute_handler (e) {
        var target = e.target;
        if (target.innerHTML.indexOf("$('#charselectdropdown').on(") != -1) {
            console.log('Removed');
            e.preventDefault();
            e.stopPropagation();
            addBack();
        }
    }
  
    w.addEventListener('beforescriptexecute', pre_script_execute_handler);

    function addBack () {
      console.log('Adding new');
      
      w.$('#charselectdropdown').on('select2:select', function (e) {
        var data = e.params.data;

        if(data.id > 0) {
          window.location = '/' + window.location.href + '?suid=' + data.id + '&serverid=2';
        }
        else {
          window.location = '/myaccount';
        }
      });
    }


}) (unsafeWindow);

我也试过用这个:

功能

replaceTargetJavascript (scriptNode) {
    var scriptSrc   = scriptNode.textContent;
    scriptSrc       = scriptSrc.replace (
        /world?suid=/,
        window.location.href + "?suid="
    );

    addJS_Node (scriptSrc);
}

checkForBadJavascripts ( [
    [false, /world?suid=/, replaceTargetJavascript]
] );

这些似乎都不适合我。谁能告诉我问题是什么?

标签: javascriptfirefoxfirefox-addonanonymous-functiongreasemonkey-4

解决方案


推荐阅读