OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview APIs used by CRWContextMenuController. | 6 * @fileoverview APIs used by CRWContextMenuController. |
7 */ | 7 */ |
8 | 8 |
9 goog.provide('__crWeb.contextMenu'); | 9 goog.provide('__crWeb.contextMenu'); |
10 | 10 |
11 /** Beginning of anonymous object */ | 11 /** Beginning of anonymous object */ |
12 (function() { | 12 (function() { |
13 | 13 |
14 /** | 14 /** |
15 * Returns the url of the image or link under the selected point. Returns an | 15 * Returns the url of the image or link under the selected point. Returns an |
16 * empty string if no links or images are found. | 16 * empty object if no links or images are found. |
17 * @param {number} x Horizontal center of the selected point. | 17 * @param {number} x Horizontal center of the selected point in web view |
18 * @param {number} y Vertical center of the selected point. | 18 * coordinates. |
| 19 * @param {number} y Vertical center of the selected point in web view |
| 20 * coordinates. |
| 21 * @param {number} webViewWidth the width of web view. |
| 22 * @param {number} webViewHeight the height of web view. |
19 * @return {!Object} An object of the form { | 23 * @return {!Object} An object of the form { |
20 * href, // URL of the link under the point | 24 * href, // URL of the link under the point |
21 * innerText, // innerText of the link, if the selected element is a link | 25 * innerText, // innerText of the link, if the selected element is a link |
22 * src, // src of the image, if the selected element is an image | 26 * src, // src of the image, if the selected element is an image |
23 * title, // title of the image, if the selected | 27 * title, // title of the image, if the selected |
24 * referrerPolicy | 28 * referrerPolicy |
25 * } | 29 * } |
26 * where: | 30 * where: |
27 * <ul> | 31 * <ul> |
28 * <li>href, innerText are set if the selected element is a link. | 32 * <li>href, innerText are set if the selected element is a link. |
29 * <li>src, title are set if the selected element is an image. | 33 * <li>src, title are set if the selected element is an image. |
30 * <li>href is also set if the selected element is an image with a link. | 34 * <li>href is also set if the selected element is an image with a link. |
31 * <li>referrerPolicy is the referrer policy to use for navigations away | 35 * <li>referrerPolicy is the referrer policy to use for navigations away |
32 * from the current page. | 36 * from the current page. |
33 * </ul> | 37 * </ul> |
34 */ | 38 */ |
35 __gCrWeb['getElementFromPoint'] = function(x, y) { | 39 __gCrWeb['getElementFromPoint'] = |
| 40 function(x, y, webViewWidth, webViewHeight) { |
| 41 var scale = getPageWidth() / webViewWidth; |
| 42 return getElementFromPointInPageCoordinates(x * scale, y * scale) |
| 43 }; |
| 44 |
| 45 /** |
| 46 * Suppresses the next click such that they are not handled by JS click |
| 47 * event handlers. |
| 48 * @type {void} |
| 49 */ |
| 50 __gCrWeb['suppressNextClick'] = function() { |
| 51 var suppressNextClick = function(evt) { |
| 52 evt.preventDefault(); |
| 53 document.removeEventListener('click', suppressNextClick, false); |
| 54 }; |
| 55 document.addEventListener('click', suppressNextClick); |
| 56 }; |
| 57 |
| 58 /** |
| 59 * Returns the url of the image or link under the selected point in page |
| 60 * coordinates. Returns an empty object if no links or images are found. |
| 61 * @param {number} x Horizontal center of the selected point in page |
| 62 * coordinates. |
| 63 * @param {number} y Vertical center of the selected point in page |
| 64 * coordinates. |
| 65 * @return {!Object} An object in the same form as |
| 66 * {@code getElementFromPoint} result. |
| 67 */ |
| 68 var getElementFromPointInPageCoordinates = function(x, y) { |
36 var hitCoordinates = spiralCoordinates_(x, y); | 69 var hitCoordinates = spiralCoordinates_(x, y); |
37 for (var index = 0; index < hitCoordinates.length; index++) { | 70 for (var index = 0; index < hitCoordinates.length; index++) { |
38 var coordinates = hitCoordinates[index]; | 71 var coordinates = hitCoordinates[index]; |
39 | 72 |
40 var element = elementFromPoint_(coordinates.x, coordinates.y); | 73 var element = elementFromPoint_(coordinates.x, coordinates.y); |
41 if (!element || !element.tagName) { | 74 if (!element || !element.tagName) { |
42 // Nothing under the hit point. Try the next hit point. | 75 // Nothing under the hit point. Try the next hit point. |
43 continue; | 76 continue; |
44 } | 77 } |
45 | 78 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 } | 135 } |
103 return result; | 136 return result; |
104 } | 137 } |
105 element = element.parentNode; | 138 element = element.parentNode; |
106 } | 139 } |
107 } | 140 } |
108 return {}; | 141 return {}; |
109 }; | 142 }; |
110 | 143 |
111 /** | 144 /** |
112 * Suppresses the next click such that they are not handled by JS click | |
113 * event handlers. | |
114 * @type {void} | |
115 */ | |
116 __gCrWeb['suppressNextClick'] = function() { | |
117 var suppressNextClick = function(evt) { | |
118 evt.preventDefault(); | |
119 document.removeEventListener('click', suppressNextClick, false); | |
120 }; | |
121 document.addEventListener('click', suppressNextClick); | |
122 }; | |
123 | |
124 /** | |
125 * Returns the margin in points around touchable elements (e.g. links for | 145 * Returns the margin in points around touchable elements (e.g. links for |
126 * custom context menu). | 146 * custom context menu). |
127 * @type {number} | 147 * @type {number} |
128 */ | 148 */ |
129 __gCrWeb['getPageWidth'] = function() { | 149 var getPageWidth = function() { |
130 var documentElement = document.documentElement; | 150 var documentElement = document.documentElement; |
131 var documentBody = document.body; | 151 var documentBody = document.body; |
132 return Math.max(documentElement.clientWidth, | 152 return Math.max(documentElement.clientWidth, |
133 documentElement.scrollWidth, | 153 documentElement.scrollWidth, |
134 documentElement.offsetWidth, | 154 documentElement.offsetWidth, |
135 documentBody.scrollWidth, | 155 documentBody.scrollWidth, |
136 documentBody.offsetWidth); | 156 documentBody.offsetWidth); |
137 }; | 157 }; |
138 | 158 |
139 /** | 159 /** |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 var metaTags = document.getElementsByTagName('meta'); | 280 var metaTags = document.getElementsByTagName('meta'); |
261 for (var i = 0; i < metaTags.length; ++i) { | 281 for (var i = 0; i < metaTags.length; ++i) { |
262 if (metaTags[i].name.toLowerCase() == 'referrer') { | 282 if (metaTags[i].name.toLowerCase() == 'referrer') { |
263 return metaTags[i].content.toLowerCase(); | 283 return metaTags[i].content.toLowerCase(); |
264 } | 284 } |
265 } | 285 } |
266 return 'default'; | 286 return 'default'; |
267 }; | 287 }; |
268 | 288 |
269 }()); // End of anonymouse object | 289 }()); // End of anonymouse object |
OLD | NEW |