|
| 1 | +/****************************************************************************** |
| 2 | + * |
| 3 | + * Copyright (c) 2017, the Perspective Authors. |
| 4 | + * |
| 5 | + * This file is part of the Perspective library, distributed under the terms of |
| 6 | + * the Apache License 2.0. The full license can be found in the LICENSE file. |
| 7 | + * |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * This injects a box into the page that moves with the mouse; Useful for |
| 12 | + * debugging. |
| 13 | + * |
| 14 | + * Lifted from https://github.com/puppeteer/puppeteer/issues/4336 |
| 15 | + */ |
| 16 | +module.exports.track_mouse = function track_mouse() { |
| 17 | + this.evaluate(() => { |
| 18 | + const CSS = ` |
| 19 | + .mouse-helper { |
| 20 | + pointer-events: none; |
| 21 | + position: absolute; |
| 22 | + top: 0; |
| 23 | + left: 0; |
| 24 | + width: 20px; |
| 25 | + height: 20px; |
| 26 | + background: rgba(0,0,0,.4); |
| 27 | + border: 1px solid red; |
| 28 | + border-radius: 0px; |
| 29 | + margin-left: -10px; |
| 30 | + margin-top: -10px; |
| 31 | + } |
| 32 | + .mouse-helper.button-1 { |
| 33 | + transition: none; |
| 34 | + background: rgba(0,0,0,0.9); |
| 35 | + } |
| 36 | + .mouse-helper.butto |
| 37 | + n-2 { |
| 38 | + transition: none; |
| 39 | + border-color: rgba(0,0,255,0.9); |
| 40 | + } |
| 41 | + .mouse-helper.button-3 { |
| 42 | + transition: none; |
| 43 | + border-radius: 4px; |
| 44 | + } |
| 45 | + .mouse-helper.button-4 { |
| 46 | + transition: none; |
| 47 | + border-color: rgba(255,0,0,0.9); |
| 48 | + } |
| 49 | + .mouse-helper.button-5 { |
| 50 | + transition: none; |
| 51 | + border-color: rgba(0,255,0,0.9); |
| 52 | + }`; |
| 53 | + const box = document.createElement("div"); |
| 54 | + box.classList.add("mouse-helper"); |
| 55 | + const styleElement = document.createElement("style"); |
| 56 | + styleElement.innerHTML = CSS; |
| 57 | + document.head.appendChild(styleElement); |
| 58 | + document.body.appendChild(box); |
| 59 | + document.addEventListener( |
| 60 | + "mousemove", |
| 61 | + event => { |
| 62 | + box.style.left = event.pageX + "px"; |
| 63 | + box.style.top = event.pageY + "px"; |
| 64 | + updateButtons(event.buttons); |
| 65 | + }, |
| 66 | + true |
| 67 | + ); |
| 68 | + document.addEventListener( |
| 69 | + "mousedown", |
| 70 | + event => { |
| 71 | + updateButtons(event.buttons); |
| 72 | + box.classList.add("button-" + event.which); |
| 73 | + }, |
| 74 | + true |
| 75 | + ); |
| 76 | + document.addEventListener( |
| 77 | + "mouseup", |
| 78 | + event => { |
| 79 | + updateButtons(event.buttons); |
| 80 | + box.classList.remove("button-" + event.which); |
| 81 | + }, |
| 82 | + true |
| 83 | + ); |
| 84 | + function updateButtons(buttons) { |
| 85 | + for (let i = 0; i < 5; i++) box.classList.toggle("button-" + i, buttons & (1 << i)); |
| 86 | + } |
| 87 | + }); |
| 88 | +}; |
0 commit comments