Hirdetés

Keresés

Új hozzászólás Aktív témák

  • btz
    addikt

    Ezt próbálgattam már. Nálam nem működik (szokás szerint)

    Először így próbáltam:

    <script type="text/javascript" >
    // Create a fake textarea
    const textAreaEle = document.createElement('textarea');

    // Reset styles
    textAreaEle.style.border = '0';
    textAreaEle.style.padding = '0';
    textAreaEle.style.margin = '0';

    // Set the absolute position
    // User won't see the element
    textAreaEle.style.position = 'absolute';
    textAreaEle.style.left = '-9999px';
    textAreaEle.style.top = `0px`;

    // Set the value
    textAreaEle.value = text;

    // Append the textarea to body
    document.body.appendChild(textAreaEle);

    // Focus and select the text
    textAreaEle.focus();
    textAreaEle.select();

    // Execute the "copy" command
    try {
    document.execCommand('copy');
    } catch (err) {
    // Unable to copy
    } finally {
    // Remove the textarea
    document.body.removeChild(textAreaEle);
    }
    </script>

    Ekkor a textAreaEle.value = text; érték nem volt definiálva így készítettem neki egy változót. var text = "tesztszöveg";

    <script type="text/javascript" >
    // Create a fake textarea
    const textAreaEle = document.createElement('textarea');

    // Reset styles
    textAreaEle.style.border = '0';
    textAreaEle.style.padding = '0';
    textAreaEle.style.margin = '0';

    // Set the absolute position
    // User won't see the element
    textAreaEle.style.position = 'absolute';
    textAreaEle.style.left = '-9999px';
    textAreaEle.style.top = `0px`;

    var text = "tesztszöveg";

    // Set the value
    textAreaEle.value = text;

    // Append the textarea to body
    document.body.appendChild(textAreaEle);

    // Focus and select the text
    textAreaEle.focus();
    textAreaEle.select();

    // Execute the "copy" command
    try {
    document.execCommand('copy');
    } catch (err) {
    // Unable to copy
    } finally {
    // Remove the textarea
    document.body.removeChild(textAreaEle);
    }
    </script>

    Hiba: Uncaught TypeError: Cannot read property 'appendChild' of null.
    Beraktam bodyba a scriptet, utána semmit nem csinált, viszont a hiba megszűnt.
    Aztán a document.body részt cseréltem document.getElementById("mainContent").appendChild(textAreaEle);-re. Így már megjelenik a textarea, benne a tesztszöveg felirattal. (A kódból kitöröltem azt a részt ami elrejti a textarea-t).

    <html>
    <body>

    <div id="mainContent">
    MAIN CONTENT<br />
    </div>
    <script type="text/javascript" >
    // Create a fake textarea
    const textAreaEle = document.createElement('textarea');

    var text = "tesztszöveg";

    // Set the value
    textAreaEle.value = text;

    // Append the textarea to body
    document.getElementById("mainContent").appendChild(textAreaEle);

    // Focus and select the text
    textAreaEle.focus();
    textAreaEle.select();

    // Execute the "copy" command
    try {
    document.execCommand('copy');
    } catch (err) {
    // Unable to copy
    } finally {
    // Remove the textarea
    //document.body.removeChild(textAreaEle);
    }
    </script>
    </body>
    <html>

    Viszont nem másol semmit a vágólapra.

    Nos. Végre megy. Kitöröltem a számomra felesleges sallangokat. Létrehoztam egy copy funkciót és egy Copy Buttont:

    <html>
    <body>

    <div id="mainContent">
    MAIN CONTENT<br />
    </div>
    <br />
    <button id="btnCopy">COPY</button>
    <script type="text/javascript" >
    // Create a fake textarea
    const textAreaEle = document.createElement('textarea');

    var text = "tesztszöveg 3333";

    // Set the value
    textAreaEle.value = text;

    // Append the textarea to body
    document.getElementById("mainContent").appendChild(textAreaEle);

    btnCopy.onclick = function () {
    textAreaEle.select();
    textAreaEle.focus();
    document.execCommand('copy');
    };

    </script>
    </body>
    <html>

    Aztán az eredeti saját kódomat így módosítottam:

    </head>
    <body>
    <b>AZ EMBED kód előnézete:</b><br />
    <iframe src="https://parentdomain.com/embed/id" id="iframe" name="iframe" scrolling="no" frameborder="0" style="width: 100%; height: 600px;"></iframe>
    <br /><br />

    <div id="myDiv">0</div>

    <script type="text/javascript" >
    function receiveMessage(event) {
    var ifrheig = (event.data.v1) + 'px';

    document.getElementById("iframe").style.height = ifrheig;

    var v1data = (event.data.v1);

    var textNode = document.createTextNode(v1data);
    document.body.appendChild(textNode);

    var toCopy = '<iframe src="https://parentdomain.com/embed/id" id="iframe" name="iframe" scrolling="no" frameborder="0" style="width: 100%; height: ' + v1data + 'px;"></iframe>';

    var innertomydiv = '<b>Embed kódja:</b><textarea rows="5" style="width: 100%;" type="text" id="code" name="code">' + toCopy + '</textarea><button id="btnCopy"><b>COPY</b></button>';

    document.getElementById("myDiv").innerHTML = innertomydiv;

    btnCopy.onclick = function () {
    document.getElementById("code").select();
    document.getElementById("code").focus();
    document.execCommand('copy');
    };

    }

    window.addEventListener("message", receiveMessage, false);

    </script>
    </body></html>

    Így a böngészőben megjelenik az Embed előnézete, alatta egy Textarea mezőben az Iframe kód (Jobb mint az input mező) a magassági értékkel, egy működő Copy gomb, ami vágólapra másolja a Iframe embed kódot, végül teszt gyanánt a magassági érték, de ez a publikus kódban már nem lesz benne.

  • nevemfel
    senior tag

    Ezt próbálgattam már. Nálam nem működik (szokás szerint)

    Először így próbáltam:

    <script type="text/javascript" >
    // Create a fake textarea
    const textAreaEle = document.createElement('textarea');

    // Reset styles
    textAreaEle.style.border = '0';
    textAreaEle.style.padding = '0';
    textAreaEle.style.margin = '0';

    // Set the absolute position
    // User won't see the element
    textAreaEle.style.position = 'absolute';
    textAreaEle.style.left = '-9999px';
    textAreaEle.style.top = `0px`;

    // Set the value
    textAreaEle.value = text;

    // Append the textarea to body
    document.body.appendChild(textAreaEle);

    // Focus and select the text
    textAreaEle.focus();
    textAreaEle.select();

    // Execute the "copy" command
    try {
    document.execCommand('copy');
    } catch (err) {
    // Unable to copy
    } finally {
    // Remove the textarea
    document.body.removeChild(textAreaEle);
    }
    </script>

    Ekkor a textAreaEle.value = text; érték nem volt definiálva így készítettem neki egy változót. var text = "tesztszöveg";

    <script type="text/javascript" >
    // Create a fake textarea
    const textAreaEle = document.createElement('textarea');

    // Reset styles
    textAreaEle.style.border = '0';
    textAreaEle.style.padding = '0';
    textAreaEle.style.margin = '0';

    // Set the absolute position
    // User won't see the element
    textAreaEle.style.position = 'absolute';
    textAreaEle.style.left = '-9999px';
    textAreaEle.style.top = `0px`;

    var text = "tesztszöveg";

    // Set the value
    textAreaEle.value = text;

    // Append the textarea to body
    document.body.appendChild(textAreaEle);

    // Focus and select the text
    textAreaEle.focus();
    textAreaEle.select();

    // Execute the "copy" command
    try {
    document.execCommand('copy');
    } catch (err) {
    // Unable to copy
    } finally {
    // Remove the textarea
    document.body.removeChild(textAreaEle);
    }
    </script>

    Hiba: Uncaught TypeError: Cannot read property 'appendChild' of null.
    Beraktam bodyba a scriptet, utána semmit nem csinált, viszont a hiba megszűnt.
    Aztán a document.body részt cseréltem document.getElementById("mainContent").appendChild(textAreaEle);-re. Így már megjelenik a textarea, benne a tesztszöveg felirattal. (A kódból kitöröltem azt a részt ami elrejti a textarea-t).

    <html>
    <body>

    <div id="mainContent">
    MAIN CONTENT<br />
    </div>
    <script type="text/javascript" >
    // Create a fake textarea
    const textAreaEle = document.createElement('textarea');

    var text = "tesztszöveg";

    // Set the value
    textAreaEle.value = text;

    // Append the textarea to body
    document.getElementById("mainContent").appendChild(textAreaEle);

    // Focus and select the text
    textAreaEle.focus();
    textAreaEle.select();

    // Execute the "copy" command
    try {
    document.execCommand('copy');
    } catch (err) {
    // Unable to copy
    } finally {
    // Remove the textarea
    //document.body.removeChild(textAreaEle);
    }
    </script>
    </body>
    <html>

    Viszont nem másol semmit a vágólapra.

    Az első példádat simán betettem a html oldalba, script tagok közé, és a firefox erre ezt írta:

    A document.execCommand('cut'/'copy') megtagadva, mert az nem egy rövid ideig futó, felhasználó által generált esemény kezelőjéből lett hívva.

    Ezután beraktam az egészet egy eseménykezelőbe, így már működik:

    [link]

  • martonx
    veterán

    Ezt próbálgattam már. Nálam nem működik (szokás szerint)

    Először így próbáltam:

    <script type="text/javascript" >
    // Create a fake textarea
    const textAreaEle = document.createElement('textarea');

    // Reset styles
    textAreaEle.style.border = '0';
    textAreaEle.style.padding = '0';
    textAreaEle.style.margin = '0';

    // Set the absolute position
    // User won't see the element
    textAreaEle.style.position = 'absolute';
    textAreaEle.style.left = '-9999px';
    textAreaEle.style.top = `0px`;

    // Set the value
    textAreaEle.value = text;

    // Append the textarea to body
    document.body.appendChild(textAreaEle);

    // Focus and select the text
    textAreaEle.focus();
    textAreaEle.select();

    // Execute the "copy" command
    try {
    document.execCommand('copy');
    } catch (err) {
    // Unable to copy
    } finally {
    // Remove the textarea
    document.body.removeChild(textAreaEle);
    }
    </script>

    Ekkor a textAreaEle.value = text; érték nem volt definiálva így készítettem neki egy változót. var text = "tesztszöveg";

    <script type="text/javascript" >
    // Create a fake textarea
    const textAreaEle = document.createElement('textarea');

    // Reset styles
    textAreaEle.style.border = '0';
    textAreaEle.style.padding = '0';
    textAreaEle.style.margin = '0';

    // Set the absolute position
    // User won't see the element
    textAreaEle.style.position = 'absolute';
    textAreaEle.style.left = '-9999px';
    textAreaEle.style.top = `0px`;

    var text = "tesztszöveg";

    // Set the value
    textAreaEle.value = text;

    // Append the textarea to body
    document.body.appendChild(textAreaEle);

    // Focus and select the text
    textAreaEle.focus();
    textAreaEle.select();

    // Execute the "copy" command
    try {
    document.execCommand('copy');
    } catch (err) {
    // Unable to copy
    } finally {
    // Remove the textarea
    document.body.removeChild(textAreaEle);
    }
    </script>

    Hiba: Uncaught TypeError: Cannot read property 'appendChild' of null.
    Beraktam bodyba a scriptet, utána semmit nem csinált, viszont a hiba megszűnt.
    Aztán a document.body részt cseréltem document.getElementById("mainContent").appendChild(textAreaEle);-re. Így már megjelenik a textarea, benne a tesztszöveg felirattal. (A kódból kitöröltem azt a részt ami elrejti a textarea-t).

    <html>
    <body>

    <div id="mainContent">
    MAIN CONTENT<br />
    </div>
    <script type="text/javascript" >
    // Create a fake textarea
    const textAreaEle = document.createElement('textarea');

    var text = "tesztszöveg";

    // Set the value
    textAreaEle.value = text;

    // Append the textarea to body
    document.getElementById("mainContent").appendChild(textAreaEle);

    // Focus and select the text
    textAreaEle.focus();
    textAreaEle.select();

    // Execute the "copy" command
    try {
    document.execCommand('copy');
    } catch (err) {
    // Unable to copy
    } finally {
    // Remove the textarea
    //document.body.removeChild(textAreaEle);
    }
    </script>
    </body>
    <html>

    Viszont nem másol semmit a vágólapra.

    Ember, szokjál már rá a jsfiddle-re

Új hozzászólás Aktív témák