ramajs

web browser automation from your browser console
git clone https://tongong.net/git/ramajs.git
Log | Files | Refs | README | LICENSE

commit e389e9b78f38367b80c734bcc20f647a846fc0ac
parent c5cba38b08d4c5ee12cf4fe796014d2262dcb08b
Author: tongong <tongong@gmx.net>
Date:   Sun, 29 Nov 2020 09:02:24 +0100

first full implementation

Diffstat:
Mrama.js | 56+++++++++++++++++++++++++++++++++++++++++++-------------
1 file changed, 43 insertions(+), 13 deletions(-)

diff --git a/rama.js b/rama.js @@ -1,9 +1,11 @@ -// no "let" or "var" here so that pasting updated versions of the code -// to the developer console does not fire redeclaration-errors +// no "let" or "var" here so that pasting updated versions of the code to the +// developer console does not fire redeclaration-errors rama = { // #################### iframe functions #################### - number: 0, // for ids -> no id gets used twice - stdparent: document.querySelector("body"), // to globally change this setting + // for ids -> no id gets used twice + number: 0, + // to make this settig globally changeable + stdparent: document.querySelector("body"), new: function (config) { // parse the settings object @@ -30,19 +32,45 @@ rama = { config.parent.appendChild(iframeTag); // create the rframe object - // is this the correct way to create an object? it works... + // Is this the correct way to create an object? it works... const rframe = { tag: iframeTag, t: iframeTag, - // d & document are added when the page loads (see below) + // .d & .document are added when the page loads (see below) w: iframeTag.contentWindow, window: iframeTag.contentWindow, name: config.name, id: config.name, - waitForReload: function () {}, - waitForUpdate: function () {}, - waitForSelector: function () {}, + waitForReload: function () { + return new Promise((resolve) => { + iframeTag.addEventListener("load", resolve, { once: true }); + }); + }, + waitFor: function (testFunction, delay) { + // - kind of hacky implementation with setInterval() ... + // - You could use MutationObservers here but that seems to be + // performance-wise the worse choice + // - when MutationObservers aren't used there is actually no + // reason to define this function on this specific object + // rather than global + return new Promise((resolve) => { + if (!delay) delay = 500; + + let interval = window.setInterval(() => { + if (testFunction(rframe)) { + window.clearInterval(interval); + resolve(); + } + }, delay); + }); + }, + waitForSelector: function (selector, delay) { + return this.waitFor( + (c) => c.d && !!c.d.querySelector(selector), + delay + ); + }, close: function () { this.tag.parentElement.removeChild(this.tag); } @@ -60,11 +88,12 @@ rama = { }, clearpage: function () { - // Not the best practice to put content outside the <body> tag - // but it enables us in this case to completely switch to the iframe window + // Not the best practice to put content outside the <body> tag but it + // enables us in this case to completely switch to the iframe window this.stdparent = document.querySelector("html"); this.loadcss( - ".rama-frame { width: 100vw; height: 100vh; position: fixed; top: 0; left: 0; } " + + ".rama-frame { width: 100vw; height: 100vh; " + + "position: fixed; top: 0; left: 0; } " + "body { display: none; } " + "html { overflow: hidden; } " ); @@ -72,7 +101,8 @@ rama = { // #################### non-iframe functions #################### loadjs: function (url) { - // modified version of script by Frank Gambino on https://stackoverflow.com/a/39008859 + // modified version of script by Frank Gambino on + // https://stackoverflow.com/a/39008859 return new Promise((resolve, reject) => { let scriptTag = document.createElement("script"); scriptTag.src = url;