"Injection works OK for text change."
This commit is contained in:
parent
fc5b74d581
commit
b4299f7d10
|
@ -1,3 +1,7 @@
|
||||||
|
import {DEBUG as debug} from '../src/common.js';
|
||||||
|
|
||||||
|
const DEBUG = debug || true;
|
||||||
|
|
||||||
export function getInjection({sessionId}) {
|
export function getInjection({sessionId}) {
|
||||||
// Notes:
|
// Notes:
|
||||||
// say() function
|
// say() function
|
||||||
|
@ -8,7 +12,19 @@ export function getInjection({sessionId}) {
|
||||||
// in future
|
// in future
|
||||||
return `
|
return `
|
||||||
{
|
{
|
||||||
if ( top === self ) {
|
const DEBUG = ${DEBUG};
|
||||||
|
const MIN_CHECK_TEXT = 3000; // min time between checking documentElement.innerText
|
||||||
|
const MIN_NOTIFY = 5000; // min time between telling controller text maybe changed
|
||||||
|
const MAX_NOTIFICATIONS = 13; // max times we will tell controller text maybe changed
|
||||||
|
const OBSERVER_OPTS = {
|
||||||
|
subtree: true,
|
||||||
|
childList: true,
|
||||||
|
characterData: true
|
||||||
|
};
|
||||||
|
const Top = globalThis.top;
|
||||||
|
let lastInnerText;
|
||||||
|
|
||||||
|
if ( Top === globalThis ) {
|
||||||
const ConsoleInfo = console.info.bind(console);
|
const ConsoleInfo = console.info.bind(console);
|
||||||
const JSONStringify = JSON.stringify.bind(JSON);
|
const JSONStringify = JSON.stringify.bind(JSON);
|
||||||
const TITLE_CHANGES = 10;
|
const TITLE_CHANGES = 10;
|
||||||
|
@ -16,6 +32,8 @@ export function getInjection({sessionId}) {
|
||||||
const TIME_MULTIPLIER = Math.E;
|
const TIME_MULTIPLIER = Math.E;
|
||||||
const sessionId = "${sessionId}";
|
const sessionId = "${sessionId}";
|
||||||
const sleep = ms => new Promise(res => setTimeout(res, ms));
|
const sleep = ms => new Promise(res => setTimeout(res, ms));
|
||||||
|
const handler = throttle(handleFrameMessage, MIN_NOTIFY);
|
||||||
|
let count = 0;
|
||||||
|
|
||||||
installTop();
|
installTop();
|
||||||
|
|
||||||
|
@ -25,7 +43,7 @@ export function getInjection({sessionId}) {
|
||||||
say({install: { sessionId, startUrl }});
|
say({install: { sessionId, startUrl }});
|
||||||
await sleep(500);
|
await sleep(500);
|
||||||
beginTitleChecks();
|
beginTitleChecks();
|
||||||
beginTextChecks();
|
beginTextNotifications();
|
||||||
console.log("Installed.");
|
console.log("Installed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,29 +79,72 @@ export function getInjection({sessionId}) {
|
||||||
ConsoleInfo(JSONStringify(thing));
|
ConsoleInfo(JSONStringify(thing));
|
||||||
}
|
}
|
||||||
|
|
||||||
function beginTextChecks() {
|
function beginTextNotifications() {
|
||||||
// listen for {textChanged:true} messages
|
// listen for {textChanged:true} messages
|
||||||
// throttle them
|
// throttle them
|
||||||
// on leading throttle edge send message to controller with
|
// on leading throttle edge send message to controller with
|
||||||
// console.info(JSON.stringify({textChanged:true}));
|
// console.info(JSON.stringify({textChanged:true}));
|
||||||
self.addEventListener('message', ({data, origin}) => {
|
self.addEventListener('message', messageParser);
|
||||||
|
|
||||||
|
console.log('Begun notifying of text changes.');
|
||||||
|
|
||||||
|
function messageParser({data, origin}) {
|
||||||
|
let source;
|
||||||
try {
|
try {
|
||||||
|
({source} = data.frameTextChangedNotification);
|
||||||
|
if ( count > MAX_NOTIFICATIONS ) {
|
||||||
|
self.removeEventListener('message', messageParser);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
count++;
|
||||||
|
handler({textChanged:{source}});
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
|
DEBUG && console.warn('could not parse message', data, e);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function beginDocumentMutationChecks() {
|
function handleFrameMessage({textChanged}) {
|
||||||
|
const {source} = textChanged;
|
||||||
|
console.log('Telling controller that text changed');
|
||||||
|
say({textChanged:true, source, count});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
beginTextMutationChecks();
|
||||||
|
|
||||||
|
function beginTextMutationChecks() {
|
||||||
// create mutation observer for text
|
// create mutation observer for text
|
||||||
// throttle output
|
// throttle output
|
||||||
// on leading throttle edge send top.postMessage({textChanged:true}, '*');
|
|
||||||
|
const observer = new MutationObserver(check, /*throttle(check, MIN_CHECK_TEXT)*/);
|
||||||
|
observer.observe(document.documentElement || document, OBSERVER_OPTS);
|
||||||
|
|
||||||
|
console.log('Begun observing text changes.');
|
||||||
|
|
||||||
|
function check() {
|
||||||
|
console.log('check');
|
||||||
|
const textMutated = document.documentElement.innerText !== lastInnerText;
|
||||||
|
if ( textMutated ) {
|
||||||
|
DEBUG && console.log('Text changed');
|
||||||
|
lastInnerText = document.documentElement.innerText;
|
||||||
|
Top.postMessage({frameTextChangedNotification:{source:location.href}}, '*');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// javascript throttle function
|
// javascript throttle function
|
||||||
|
// source: https://stackoverflow.com/a/59378445
|
||||||
|
function throttle(func, timeFrame) {
|
||||||
|
var lastTime = 0;
|
||||||
|
return function (...args) {
|
||||||
|
var now = new Date();
|
||||||
|
if (now - lastTime >= timeFrame) {
|
||||||
|
func.apply(this, args);
|
||||||
|
lastTime = now;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue