1

我正在尝试修改一个WordPress 插件,该插件在按下按钮时会激活游戏脚本。我希望能够通过按键组合(Shift + Control + F)来激活它。

我尝试将整个脚本包装在按键功能中,但是,这不起作用。我已经确认脚本是通过控制台日志加载的,但按下组合键不会做任何事情。

原始代码:

PHP


<?php

...

/* Insert the button*/
  switch ($asteroids_buttonopt) {
  case "push-1":
    echo  '<div><p style="text-align: center;">
           <a href="#" onclick="'.$asteroids_start.'"><button>Click to Play Asteroids!!!</button>
           </a></p></div>';
  break;

...

  }

?>

JS

  function getInternetExplorerVersion()
  // Returns the version of Internet Explorer or a -1
  // (indicating the use of another browser).
  {
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer')
    {
      var ua = navigator.userAgent;
      var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
      if (re.exec(ua) != null)
        rv = parseFloat( RegExp.$1 );
    }
    return rv;
  }


  function startAsteroids(color,address) { 
    var ver = getInternetExplorerVersion();
    if(ver>=0){
      color = typeof(color) != 'undefined' ? color : 'black';
      document.onkeydown = function(ev) {   
      var key;
      ev = ev || event;
      key = ev.keyCode;
          if(key == 37 || key == 38 || key == 39 || key == 40) {
          //e.cancelBubble is supported by IE - this will kill the bubbling process.
          ev.cancelBubble = true;
          ev.returnValue = false;
          }      
      }
      var s =document.createElement('script');
      s.type='text/javascript'
      document.body.appendChild(s);
      s.src = address;
      void(0);
      return false; 
    }
    else{
      color = typeof(color) != 'undefined' ? color : 'black';
      var s =document.createElement('script');
      s.type='text/javascript'
      document.body.appendChild(s);
      s.src = address;
      void(0);
      return false; 
    }
  }

任何帮助将不胜感激。最初的插件“ Asteroid Widget ”在 8 年前就被废弃了,所以我无法向开发人员寻求帮助。

4

1 回答 1

2

您应该将以下 onkeypress 函数添加到 JS 文件的末尾,而不是包装整个页面内容。

所需功能

    window.onkeypress=function(e){
    if(e.ctrlKey && e.shiftKey && e.code==='KeyF' ){
      startAsteroids('','/wp-content/plugins/asteroids-widget/gears/play-asteroids-yellow.min.js');
    }
  }

完成生成的文件

  function getInternetExplorerVersion()
  // Returns the version of Internet Explorer or a -1
  // (indicating the use of another browser).
  {
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer')
    {
      var ua = navigator.userAgent;
      var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
      if (re.exec(ua) != null)
        rv = parseFloat( RegExp.$1 );
    }
    return rv;
  }


  function startAsteroids(color,address) { 
    var ver = getInternetExplorerVersion();
    if(ver>=0){
      color = typeof(color) != 'undefined' ? color : 'black';
      document.onkeydown = function(ev) {   
      var key;
      ev = ev || event;
      key = ev.keyCode;
          if(key == 37 || key == 38 || key == 39 || key == 40) {
          //e.cancelBubble is supported by IE - this will kill the bubbling process.
          ev.cancelBubble = true;
          ev.returnValue = false;
          }      
      }
      var s =document.createElement('script');
      s.type='text/javascript'
      document.body.appendChild(s);
      s.src = address;
      void(0);
      return false; 
    }
    else{
      color = typeof(color) != 'undefined' ? color : 'black';
      var s =document.createElement('script');
      s.type='text/javascript'
      document.body.appendChild(s);
      s.src = address;
      void(0);
      return false; 
    }
  }
    window.onkeypress=function(e){
    if(e.ctrlKey && e.shiftKey && e.code==='KeyF' ){
      startAsteroids('','/wp-content/plugins/asteroids-widget/gears/play-asteroids-yellow.min.js');
    }
  }
于 2020-04-16T03:01:25.827 回答