Path: blob/master/ClickIsTrusted-master/contentScript.js
1634 views
console.log("ClickIsTrusted: loading contentscript (and here is the debug link).");12function makeModifiersInteger(e) {3let res = 0;4if (e.getModifierState("Alt"))5res += 1;6if (e.getModifierState("Control"))7res += 2;8if (e.getModifierState("Meta"))9res += 4;10if (e.getModifierState("Shift"))11res += 8;12return res;13}1415function convertMouseType(type) {16if (type.startsWith("mousedown"))17return "mousePressed";18if (type.startsWith("mouseup"))19return "mouseReleased";20if (type.startsWith("mousemove"))21return "mouseMoved";22if (type.startsWith("wheel"))23return "mouseWheel";24throw new Error(`The ${type} event cannot be replicated by the ClickIsTrusted extension.`);25}2627function convertButton(button) {28if (button === 0)29return "left";30if (button === 1)31return "middle";32if (button === 2)33return "right";34if (button === 3)35return "back";36if (button === 4)37return "forward";38}3940function convertMouseEvent(e) {41return {42type: convertMouseType(e.type),43modifiers: makeModifiersInteger(e),44buttons: e.buttons,45button: convertButton(e.button),46x: e.clientX,47y: e.clientY,48deltaX: e.deltaX,49deltaY: e.deltaY,50clickCount: 1, // needed to make composed `click` events51// deltaMode: e.deltaMode, //isn't active in the interface.52// timeStamp: e.timeStamp //todo include this one??53// pointerType: "mouse" || "pen" //todo enable this one??54};55}5657function convertTouchType(type) {58if (type.startsWith("touchstart"))59return "touchStart";60if (type.startsWith("touchmove"))61return "touchMove";62if (type.startsWith("touchend"))63return "touchEnd";64if (type.startsWith("touchcancel"))65return "touchcancel";66throw new Error(`The ${type} event cannot be replicated by the ClickIsTrusted extension.`);67}6869//todo this is untested and probably doesn't work. see eventToObject.js70function convertTouchPoint(t) {71return {72x: t.clientX,73y: t.clientY,74radiusX: t.radiusX,75radiusY: t.radiusY,76rotationAngle: t.rotationAngle,77force: t.force,78id: t.identifier79};80}8182function convertTouchEvent(e) {83return {84type: convertTouchType(e.type),85modifiers: makeModifiersInteger(e),86touchPoints: e.touches.map(convertTouchPoint),87// timeStamp: e.timeStamp //todo include this one??88}89}9091function convertKeyType(type) {92if (type === "keydown-is-trusted")93return "keyDown";94if (type === "keyup-is-trusted")95return "keyUp";96if (type === "rawkeydown-is-trusted")97return "rawKeyDown";98if (type === "char-is-trusted")99return "char";100throw new Error(`The ${e.type} event cannot be replicated by the ClickIsTrusted extension.`);101}102103function convertKeyEvent(e) {104return {105type: convertKeyType(e.type),106modifiers: makeModifiersInteger(e),107key: e.key,108code: e.code,109location: e.location,110autoRepeat: e.repeat,111112text: e.text,113keyIdentifier: e.keyIdentifier,114unmodifiedText: e.unmodifiedText,115isKeyPad: e.isKeyPad,116isSystemKey: e.isSystemKey,117nativeVirtualKeyCode: e.nativeVirtualKeyCode,118windowsVirtualKeyCode: e.windowsVirtualKeyCode,119120// timeStamp: e.timeStamp //todo include this one??121};122}123124function convertInputEvent(e) {125if (!e.type.startsWith("beforeinput"))126throw new Error(`The ${e.type} event cannot be replicated by the ClickIsTrusted extension.`);127return {128type: e.type,129text: e.data,130// timeStamp: e.timeStamp //todo include this one??131};132}133134//att 1. calling chrome.runtime cannot be done from inside the event listener. (a different 'this' context?? don't know).135function sendMessage(e) {136let message;137if (e instanceof MouseEvent)138message = convertMouseEvent(e);139else if (e instanceof TouchEvent)140message = convertTouchEvent(e);141else if (e instanceof KeyboardEvent)142message = convertKeyEvent(e);143else if (e instanceof InputEvent)144message = convertInputEvent(e);145else146throw new Error("a script has tried to send a bad message: ", e);147console.log("Passing native event request to background.js: ", message);148chrome.runtime.sendMessage(message);149}150151function manInTheMiddle(event) {152console.log("received native event request from web page:", event);153event.stopImmediatePropagation();154event.preventDefault();155sendMessage(event);156}157158window.addEventListener("mousedown-is-trusted", manInTheMiddle);159window.addEventListener("mousemove-is-trusted", manInTheMiddle);160window.addEventListener("mouseup-is-trusted", manInTheMiddle);161window.addEventListener("wheel-is-trusted", manInTheMiddle);162window.addEventListener("keydown-is-trusted", manInTheMiddle);163window.addEventListener("keyup-is-trusted", manInTheMiddle);164window.addEventListener("rawkeydown-is-trusted", manInTheMiddle);165window.addEventListener("char-is-trusted", manInTheMiddle);166window.addEventListener("beforeinput-is-trusted", manInTheMiddle);167168