Adrià Vilanova MartÃnez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | // This counts copy and paste events. |
| 6 | |
| 7 | function labelForElement(el) { |
| 8 | let label = el.localName; |
| 9 | if (el.id) { |
| 10 | label = label + '#' + el.id; |
| 11 | } |
| 12 | return label; |
| 13 | } |
| 14 | |
| 15 | window.addEventListener('copy', function(evt) { |
| 16 | const label = labelForElement(evt.srcElement); |
| 17 | const len = window.getSelection().toString().length; |
| 18 | ga('send', 'event', window.location.pathname, 'copy', label, len); |
| 19 | }); |
| 20 | |
| 21 | window.addEventListener('paste', function(evt) { |
| 22 | const label = labelForElement(evt.srcElement); |
| 23 | const text = evt.clipboardData.getData('text/plain'); |
| 24 | const len = text ? text.length : 0; |
| 25 | ga('send', 'event', window.location.pathname, 'paste', label, len); |
| 26 | }); |