/
githubmirror
/
amphtml
Обзор
Документация
Войти
/
githubmirror
/
amphtml
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
test/fixtures/served/iframe-intersection.html
59 строк
1 KB
Dima Voytenko
Avoid false-positive amp- messages in 3p (#7440)
09 фев 2017, 04:09
09 фев 2017, 04:09
218b224
Код
Авторство
О чём код?
<!doctype html> <body style="background-color: red"> Iframe with Inersection <textarea id=log-el style="width:100%; height: 50vh;"></textarea> <script> var startTime = Date.now(); // Default values for non-3P iframe. var sentinel = 'amp'; var ampWindow = parent; // Detect 3P sentinel in the location.hash. var hashMatch = location.hash.match(/amp-3p-sentinel=((\d+)-\d+)/); if (hashMatch) { sentinel = hashMatch[1]; // Depth is measured from window.top. var depth = Number(hashMatch[2]); var ancestors = []; for (var win = window; win && win != win.parent; win = win.parent) { // Add window keeping the top-most one at the front. ancestors.unshift(win.parent); } ampWindow = ancestors[depth]; } function log(m) { var el = document.getElementById('log-el'); el.value = el.value + '\n[' + (Date.now() - startTime) + '] ' + (m || ''); } window.addEventListener('message', function(event) { if (event.data) { if (event.data.type == 'intersection') { log('Received intersection message'); parent.parent./*OK*/postMessage({ sentinel: sentinel, changes: event.data.changes, type: 'received-intersection' }, '*'); } } }); function sendPostMessage() { ampWindow./*OK*/postMessage({ sentinel: sentinel, type: 'send-intersections' }, '*'); } sendPostMessage(); sendPostMessage(); // send twice to test no double registration // Send foreign message. ampWindow./*OK*/postMessage('amp-other', '*'); </script> </body>