Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Javascript: Problem, I solved.

Convert string into an array

const strToArr = (str = "", delimiter = " ") => String(str).split(delimiter).filter(Boolean);
strToArr(document.URL, "/"); // return an []

Resize Google user's profile photo

const resizeImg = (size, src) => {
    src = String(src);

    if (src.endsWith(".jpg")) {
        src += `?sz=${ size }`;
    } else {
        src += `=s${ size }-c`;
    }

    return src;
};
resizeImg(32, "user/photo/url"); // return resized photo.

Web Share API

<button>Share</button>
const share = (buttonShare) => {
    buttonShare.addEventListener("click", () => {
        if ("share" in navigator) {
            navigator.share({
                title: document.title,
                text: document.querySelector("meta[name=description]").content,
                url: document.URL
            }).then(() => console.info("Done")).catch((err) => console.error(err));
        } else {
            const input = document.createElement("input");
            input.value = document.URL;
            input.select();
            document.body.appendChild(input);
            document.execCommand("copy");
            document.body.removeChild(input);
            console.info("Done");
        }
    });
};
const button = document.querySelector("button");
share(button) // return options on mobile phone or copy current url to clipboard on desktop.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment