Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 不工作的Chrome Extension的向下键_Javascript_Jquery_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript 不工作的Chrome Extension的向下键

Javascript 不工作的Chrome Extension的向下键,javascript,jquery,google-chrome,google-chrome-extension,Javascript,Jquery,Google Chrome,Google Chrome Extension,我一直在为谷歌扩展的想法而挣扎,而你一如既往地是我最后的希望!:) 嗯,我想点击我的chrome扩展上的按钮,这将导致页面扩展正在运行的按键模拟 我认为chrome在我的想法中有一些安全问题,它会阻止键盘模拟(使事件为假)并删除哪个属性 我写的函数在上面运行得很好,但是chrome扩展以不同的方式完成了它 以下是内容脚本文件: chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { if(

我一直在为谷歌扩展的想法而挣扎,而你一如既往地是我最后的希望!:)

嗯,我想点击我的chrome扩展上的按钮,这将导致页面扩展正在运行的按键模拟

我认为chrome在我的想法中有一些安全问题,它会阻止键盘模拟(使事件为假)并删除哪个属性

我写的函数在上面运行得很好,但是chrome扩展以不同的方式完成了它

以下是内容脚本文件:

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
  if(request.action == "scrollToTop"){

  }
  else if(request.action == "scrollToBottom"){

  }
  else if(request.action == "enter"){
    triggerKeyboardEvent(document,13);
  }

  function triggerKeyboardEvent(el, keyCode){
    var event = new Event("keydown", {"bubbles":true, "cancelable":true});
    event.which = keyCode;
    el.dispatchEvent(event);
  }

});
chrome.runtime.sendMessage({action : "show"});
JSFIDLE上的日志写入:

Event {isTrusted: false, which: 13}
登录网站:

document.addEventListener('keydown',function (e) {
      console.log(e)
}
写的只是:

Event {isTrusted: false}

感谢@BG101和@Rob W,我发现解决方案是脚本注入

唯一的问题是,根据KeyboardEvent.initKeyboardEvent()是去润滑的,所以我将代码替换为:

var event = new Event(event, {"bubbles":true, "cancelable":true});
另外,由于我希望触发器在文档上运行,我删除了元素选择器。以下是我得到的:

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
  if(request.action == "scrollToTop"){
    triggerKeyboardEventOnDocument("keydown",38);

  }
  else if(request.action == "scrollToBottom"){
    triggerKeyboardEventOnDocument("keydown",40);

  }
  else if(request.action == "enter"){
    triggerKeyboardEventOnDocument("keydown",13);
  }

  function triggerKeyboardEventOnDocument(event, keyCode){
    var script = document.createElement('script');

    script.textContent = '(' + function(event, charCode) {

        //Create Event
        var event = new Event(event, {"bubbles":true, "cancelable":true});

        // Define custom values
        // This part requires the script to be run in the page's context
        var getterCode = {get: function() {return charCode}};
        var getterChar = {get: function() {return String.fromCharCode(charCode)}};
        Object.defineProperties(event, {
          charCode: getterCode,
          which: getterCode,
          keyCode: getterCode, // Not fully correct
          key: getterChar,     // Not fully correct
          char: getterChar
        });
        document.dispatchEvent(event);
      } + ')(' + '\"' + event + '\", '+ keyCode + ')';

    (document.head||document.documentElement).appendChild(script);
    script.parentNode.removeChild(script);
  }

});
chrome.runtime.sendMessage({action : "show"});

我有这个问题,我通过做哇来解决它,非常感谢朋友!我更改了一些内容,并将其添加为答案!