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