Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 (function() { | 1 (function() { |
| 2 | 2 |
| 3 // Gets the offset of an element in coordinates relative to the top-level frame. | |
| 4 function offset(element) { | |
| 5 const rect = element.getBoundingClientRect(); | |
| 6 const iframe = element.ownerDocument.defaultView.frameElement; | |
| 7 if (!iframe) | |
| 8 return { left: rect.left, top: rect.top }; | |
|
Mike West
2017/02/24 11:37:16
I must be getting old, but the `{}` for the object
foolip
2017/02/24 15:12:58
Ah, this does look a bit, more {} it is.
| |
| 9 const style = getComputedStyle(iframe); | |
|
Mike West
2017/02/24 11:37:16
I don't think you actually use this. Is it just to
foolip
2017/02/24 15:12:58
It's used as style[prop] inside the get function.
| |
| 10 function get(prop) { | |
|
Mike West
2017/02/24 11:37:16
Hrm. I don't know if we have a style-guide about n
foolip
2017/02/24 15:12:58
Didn't think of that, done. (I also threw in some
| |
| 11 return +/[\d.]*/.exec(style[prop])[0]; | |
| 12 } | |
| 13 const outerOffset = offset(iframe); | |
| 14 return { | |
| 15 left: outerOffset.left + get('borderLeftWidth') + get('paddingLeft') + rect. left, | |
| 16 top: outerOffset.top + get('borderTopWidth') + get('paddingTop') + rect.top | |
| 17 }; | |
| 18 } | |
| 19 | |
| 3 function click(button) { | 20 function click(button) { |
| 4 var rect = button.getBoundingClientRect(); | 21 var pos = offset(button); |
| 5 eventSender.mouseMoveTo(rect.left, rect.top); | 22 eventSender.mouseMoveTo(Math.ceil(pos.left), Math.ceil(pos.top)); |
| 6 eventSender.mouseDown(); | 23 eventSender.mouseDown(); |
| 7 eventSender.mouseUp(); | 24 eventSender.mouseUp(); |
| 8 } | 25 } |
| 9 | 26 |
| 10 var observer = new MutationObserver(mutations => { | 27 var observer = new MutationObserver(mutations => { |
| 11 for (var mutation of mutations) { | 28 for (var mutation of mutations) { |
| 12 for (var node of mutation.addedNodes) { | 29 for (var node of mutation.addedNodes) { |
| 13 if (node.localName == 'button') | 30 if (node.localName == 'button') |
| 14 click(node); | 31 click(node); |
| 15 else if (node.localName == 'iframe') | 32 else if (node.localName == 'iframe') |
| 16 observe(node.contentDocument); | 33 observe(node.contentDocument); |
| 17 } | 34 } |
| 18 } | 35 } |
| 19 }); | 36 }); |
| 20 | 37 |
| 21 function observe(target) { | 38 function observe(target) { |
| 22 observer.observe(target, { childList: true, subtree: true }); | 39 observer.observe(target, { childList: true, subtree: true }); |
| 23 } | 40 } |
| 24 | 41 |
| 25 // Handle what's already in the document. | 42 // Handle what's already in the document. |
| 26 for (var button of document.getElementsByTagName('button')) { | 43 for (var button of document.getElementsByTagName('button')) { |
| 27 click(button); | 44 click(button); |
| 28 } | 45 } |
| 29 for (var iframe of document.getElementsByTagName('iframe')) { | 46 for (var iframe of document.getElementsByTagName('iframe')) { |
| 30 observe(iframe.contentDocument); | 47 observe(iframe.contentDocument); |
| 48 iframe.addEventListener('load', () => { | |
| 49 observe(iframe.contentDocument); | |
| 50 }); | |
| 31 } | 51 } |
| 32 | 52 |
| 33 // Observe future changes. | 53 // Observe future changes. |
| 34 observe(document); | 54 observe(document); |
| 35 | 55 |
| 36 })(); | 56 })(); |
| OLD | NEW |