Estoy tratando de hacer mi propia secuencia de comandos de usuario para https://mlwbd.ltd/movie/don-2022
Deseo copiar el valor de la entrada oculta especificada
preTag = document.getElementsByName("FU"); p = preTag[0]; console.log(p); Ekra = document.getElementsByClassName("linktabs"); q = Ekra[0]; console.log(q); function copy(ele) { let temp = document.createElement('textarea'); document.body.appendChild(temp); temp.value = ele.textContent; temp.select(); document.execCommand('copy'); temp.remove(); } btn = document.createElement("button"); btn.innerHTML = "copy" btn.onclick = function(){ copy("p"); }; q.insertBefore(document.createElement("br"), q.childNodes[0]) q.insertBefore(btn, q.childNodes[0])El código HTML es
tipo de entrada = "oculto" nombre = "FU" valor = "https://songslyric.site/links/46905/"Quiero copiar el valor de name="FU" cuando hago clic en el botón. El código que pegué se crea a partir de fragmentos de Google Chrome. Por favor, ayúdame.
Esto lo ayudará a comenzar:
// ==UserScript== // @name mlwbd.ltd // @namespace http://tampermonkey.net/ // @version 0.1 // @match https://mlwbd.ltd/movie/* // @grant none // ==/UserScript== (function() { 'use strict'; const $ = document.querySelector.bind(document); $('body').insertAdjacentHTML('beforeend', init_css() ); setTimeout(() => { $('#mybutt').addEventListener('click', () => { //alert('hi'); const fu = $('#download table form input[name=FU]'); //console.log(fu.value); copy2clip(fu); }); },500); })(); function init_css(){ return ` <button id="mybutt">Copy2Clip</button> <style id="myInitCss"> #mybutt{position:absolute;top:90px;left:45%;height:30px;width:120px;} #mybutt{background:#0073ea;color:white;padding-top:5px;text-align:center;} #mybutt{z-index:99999;} </style> `; } function copy2clip(ele) { let temp = document.createElement('textarea'); document.body.appendChild(temp); temp.value = ele.value; temp.select(); document.execCommand('copy'); temp.remove(); }$ , este código no es jQuery (es javascript vainilla)alert() y console.log para ver cómo funcionan las cosas en ese momento.Prueba esto en tu otra pregunta:
// ==UserScript== // @name adsgo.digital // @namespace http://tampermonkey.net/ // @version 0.1 // @include https://adsgo.digital/ // @include https://adsgo.digital/* // @grant none // ==/UserScript== (function() { 'use strict'; const $ = document.querySelector.bind(document); $('body').insertAdjacentHTML('beforeend', init_css() ); setTimeout(() => { $('#mybutt').addEventListener('click', () => { //alert('hi'); const fu = $('.posts-dynamic.posts-container form input[name=FU6]'); //console.log(fu.value); copy2clip(fu); }); },500); })(); function init_css(){ return ` <button id="mybutt">Copy2Clip</button> <style id="myInitCss"> #mybutt{position:absolute;top:90px;left:45%;height:30px;width:120px;} #mybutt{background:#0073ea;color:white;padding-top:5px;text-align:center;} #mybutt{z-index:99999;} </style> `; } function copy2clip(ele) { let temp = document.createElement('textarea'); document.body.appendChild(temp); temp.value = ele.value; temp.select(); document.execCommand('copy'); temp.remove(); }Puede comparar esta respuesta con la anterior y notará que solo ha cambiado una línea.
El tema a estudiar para entender cómo hacer referencia al scaffolding HTML como lo hice yo se llama: "Selectores CSS". La directiva JavaScript querySelector() usa selectores CSS para apuntar específicamente a la etiqueta HTML deseada.