未使用Chrome扩展程序的Keydown [英] Keydown Which Not Working Chrome Extension

查看:114
本文介绍了未使用Chrome扩展程序的Keydown的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力想到Google扩展的想法,而且你一如既往是我的最后希望! :))

I've been struggling with my idea of google extension, and you as always are the last hope of mine ! :))

好的,我想单击我的Chrome扩展上的按钮,这会导致页面扩展中的keydown模拟正在运行。

Well, I want to click the button on my chrome extension that will cause keydown simulation on the page extension is running.

我认为chrome在我的想法上存在一些安全问题,它会阻止键盘模拟(使事件isTrusted:false)并删除哪些属性。

I think chrome has some safety issues on my idea, that blocks the keyboard simulation (makes event isTrusted:false) and deletes which property.

我写的函数在 jsfiddle 上工作正常,但看起来chrome扩展以不同的方式进行。

The function I wrote works fine on jsfiddle , but it appears that chrome extension does it in a different manner.

以下是内容脚本文件:

Here is the content script file :

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"});

jsFiddle的日志写道:

The log on jsFiddle writes:

Event {isTrusted: false, which: 13}

(e){
console.log(e)

The log on website:

document.addEventListener('keydown',function (e) {
      console.log(e)
}

只写:

writes just:

Event {isTrusted: false}


推荐答案

感谢@ BG101和@Rob WI发现解决方案是脚本注入!

Thanks to @BG101 and @Rob W I found out that solution is script injection!

唯一依据 MDN KeyboardEvent.initKeyboardEvent()被删除,所以我用下面的代码替换了代码:

the only thing was that according to MDN KeyboardEvent.initKeyboardEvent() is depricated, so I replaced the code with:

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

另外,因为我想让触发器在文档上运行,所以我删除了元素选择器的东西。这里是我得到的:

Also, as I wanted the trigger to run on document, I removed the element selector things. Here is what I got:

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"});

这篇关于未使用Chrome扩展程序的Keydown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆