Skip to content

Instantly share code, notes, and snippets.

@remram44
Last active September 13, 2024 10:33
Show Gist options
  • Save remram44/cf9330ce5805bb3413f20f9fbee25d15 to your computer and use it in GitHub Desktop.
Save remram44/cf9330ce5805bb3413f20f9fbee25d15 to your computer and use it in GitHub Desktop.
Embed HTML in Google Slides
// ==UserScript==
// @name Google Slides embedder
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Shows an iframe in Google Slides presentation to fake embedding
// @author Remi Rampin
// @match https://docs.google.com/presentation/d/*
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant none
// ==/UserScript==
(function() {
'use strict';
var injectFrame;
window.setInterval(function() {
if(injectFrame && !injectFrame.parentNode) {
console.log("Frame is gone");
injectFrame = undefined;
}
var presoFrame = document.getElementsByClassName('punch-present-iframe')
if(!presoFrame || !presoFrame.length) {
return;
}
presoFrame = presoFrame[0];
console.log("found punch-present-iframe", presoFrame);
if(!injectFrame) {
injectFrame = presoFrame.contentDocument.createElement('iframe');
injectFrame.setAttribute('src', 'https://spire.remram.fr/');
injectFrame.setAttribute('width', '400');
injectFrame.setAttribute('height', '400');
injectFrame.setAttribute('style', 'position: absolute; z-index: 300;');
injectFrame.className = "rr4_inject_frame";
presoFrame.contentDocument.body.appendChild(injectFrame);
console.log("created iframe", injectFrame);
}
var texts = presoFrame.contentDocument.getElementsByTagName('text');
console.log("", texts.length, "text elements");
var targetText;
for(var i = 0; i < texts.length; ++i) {
console.log("text:", texts[i].textContent);
if(texts[i].textContent === 'Embedding...') {
targetText = texts[i];
}
}
if(targetText) {
console.log("Found target text", targetText);
var rect = targetText.getClientRects()[0];
injectFrame.setAttribute('style', 'position: absolute; z-index: 300; left: ' + Math.round(rect.left) + 'px; top: ' + Math.round(rect.top) + 'px;');
} else {
console.log("deleting iframe");
injectFrame.parentNode.removeChild(injectFrame);
injectFrame = undefined;
}
}, 800);
})();
@soyuka
Copy link

soyuka commented Sep 13, 2024

The setInterval is only useful in preview mode, suggestion: only register the interval in presentation mode, also then you're inside the iframe you can just use window.document:

(function() {
    'use strict';

    var injectFrame;
    var isPresentationMode = document.getElementsByClassName('punch-viewer-container');
    if (!isPresentationMode.length) {
        return;
    }

    window.setInterval(function() {
        if(injectFrame && !injectFrame.parentNode) {
            console.log("Frame is gone");
            injectFrame = undefined;
        }

        if(!injectFrame) {
            injectFrame = document.createElement('iframe');
            injectFrame.setAttribute('src', 'https://edge-side-api.rocks/');
            injectFrame.setAttribute('width', '1920');
            injectFrame.setAttribute('height', '1080');
            injectFrame.setAttribute('style', 'position: absolute; z-index: 300;');
            injectFrame.className = "rr4_inject_frame";
            document.body.appendChild(injectFrame);
            console.log("created iframe", injectFrame);
        }

        var texts = document.getElementsByTagName('text');
        //console.log("", texts.length, "text elements");
        var targetText;
        for(var i = 0; i < texts.length; ++i) {
            console.log("text:", texts[i].textContent);
            if(texts[i].textContent === 'Embedding...') {
                targetText = texts[i];
            }
        }

        if(targetText) {
            //console.log("Found target text", targetText);
            var rect = targetText.getClientRects()[0];
            injectFrame.setAttribute('style', 'position: absolute; z-index: 300; left: ' + Math.round(rect.left) + 'px; top: ' + Math.round(rect.top) + 'px;');
        } else {
            console.log("deleting iframe");
            injectFrame.parentNode.removeChild(injectFrame);
            injectFrame = undefined;
        }
    }, 800);
})();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment