cvimrc

本体

構造

content_scriptからRUNTIME(hoge)を呼ぶとbackground_scripts/actions.jsの_.hogeが呼ばれる。さらにClipboard.fuga()などが呼ばれるとbackground_scripts/clipboard.jsの関数が呼ばれる。

HTMLフォーマットなコピーをするPR

javascript - How to copy HTML formatted text on to Clipboard from Google Chrome extension? - Stack Overflowにやり方が書いてある。

Firefox

IME使用時のキーイベントを無視するPR

isComposing

isComposingを使うとIMEを使っているかがわかる

compositionstart, compositionend

isComposingがない場合はcompositionstart, compositionendを使う。

KeyboardEvent.code - Web API インターフェイス | MDNにあったJSFiddleの例を参考にcompositionstart, compositionendの動作を確認するコードを作った

ChromeとFirefoxでaとEnterを押したときのイベントを確認。Firefoxだとうまくいかない。

// chrome
/// IME off
KeyboardEvent keydown: key='a' | code='KeyA' | comp=false
KeyboardEvent keypress: key='a' | code='KeyA'
KeyboardEvent keyup: key='a' | code='KeyA' | comp=false
KeyboardEvent keydown: key='Enter' | code='Enter' | comp=false
KeyboardEvent keypress: key='Enter' | code='Enter'
KeyboardEvent keyup: key='Enter' | code='Enter' | comp=false

/// IME on
KeyboardEvent keydown: key='a' | code='KeyA' | comp=false
KeyboardEvent keyup: key='a' | code='KeyA' | comp=true
KeyboardEvent keydown: key='Enter' | code='Enter' | comp=true
KeyboardEvent keyup: key='Enter' | code='Enter' | comp=false

// FF
/// IME off
KeyboardEvent keydown: key='a' | code='KeyA' | comp=false
KeyboardEvent keypress: key='a' | code='KeyA'
KeyboardEvent keyup: key='a' | code='KeyA' | comp=false
KeyboardEvent keydown: key='Enter' | code='Enter' | comp=false
KeyboardEvent keypress: key='Enter' | code='Enter'
KeyboardEvent keyup: key='Enter' | code='Enter' | comp=false

/// IME on
KeyboardEvent keydown: key='a' | code='KeyA' | comp=false
KeyboardEvent keyup: key='Enter' | code='Enter' | comp=false

JSFiddleに書いたコード

HTML

<p>Press keys on the keyboard to see what the KeyboardEvent's key and code
   values are for each one.</p>
   <textarea></textarea>
<div id="output">
</div>

JavaScript

window.addEventListener("keydown", function(event) {
  let str = "KeyboardEvent keydown: key='" + event.key + "' | code='" +
            event.code + "' | comp=" + onComposition;
  let el = document.createElement("span");
  el.innerHTML = str + "<br/>";
 
  document.getElementById("output").appendChild(el);
}, true);
window.addEventListener("keyup", function(event) {
  let str = "KeyboardEvent keyup: key='" + event.key + "' | code='" +
            event.code + "' | comp=" + onComposition;
  let el = document.createElement("span");
  el.innerHTML = str + "<br/>";
 
  document.getElementById("output").appendChild(el);
}, true);
window.addEventListener("keypress", function(event) {
  let str = "KeyboardEvent keypress: key='" + event.key + "' | code='" +
            event.code + "'";
  let el = document.createElement("span");
  el.innerHTML = str + "<br/>";
 
  document.getElementById("output").appendChild(el);
}, true);
window.addEventListener('compositionstart', function () {
          onComposition = true;
      });
window.addEventListener('compositionend', function () {
          onComposition = false;
      });

keycodeはdeprecated(KeyboardEvent - Web APIs | MDN)。