Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How can I grab all the code client-side for P5 Live? #3233

Open
TiborUdvari opened this issue Sep 18, 2024 · 1 comment
Open

Question: How can I grab all the code client-side for P5 Live? #3233

TiborUdvari opened this issue Sep 18, 2024 · 1 comment

Comments

@TiborUdvari
Copy link

Increasing Access

Unsure

Feature enhancement details

I'm working on a bookmarklet to make copying code over to P5 Live easier. The tricky part is that P5 Live loads everything into one file, with a special syntax for loading libraries. I’m wondering if there’s a way, using just vanilla JS, to grab all the code as a string from index.html, sketch.js, and any other files on the client side, assuming the user is already logged in.

@Jatin24062005
Copy link

@TiborUdvari to acheive to get p5 live code to you bookmarklet you can use document keyword.

Here is some Code snippet you can use
`javascript:(function() {
/**
* Extracts all inline and external scripts from the page.
* Inline scripts are captured as code, while external libraries are noted with their URLs.
*/
function extractScripts() {
let scripts = document.querySelectorAll("script");
let extracted = "";

    scripts.forEach(script => {
        if (script.src) {
            // If the script has a 'src' attribute, note its URL as an external library.
            extracted += `// External Library: ${script.src}\n`;
        } else {
            // If it's an inline script, capture its content.
            extracted += `\n// Inline Script Content:\n${script.textContent}\n`;
        }
    });

    return extracted;
}

/**
 * Extracts the entire HTML content of the page.
 * This includes the full document structure, styles, and any inline scripts.
 */
function extractHtml() {
    return document.documentElement.outerHTML;
}

/**
 * Copies the given text to the clipboard and notifies the user.
 * @param {string} text - The text content to be copied.
 */
function copyToClipboard(text) {
    navigator.clipboard.writeText(text).then(() => {
        alert("Code copied to clipboard! You can now paste it into P5 Live.");
    }).catch(err => console.error("Error copying text: ", err));
}

// Combine extracted HTML and script content into a single string.
let fullCode = "/* Extracted HTML */\n" + extractHtml() + "\n\n/* Extracted Scripts */\n" + extractScripts();

// Copy the combined code to the clipboard.
copyToClipboard(fullCode);

})();
`

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

No branches or pull requests

2 participants