así que soy un principiante y comencé a hacer una extensión de Chrome solo por diversión. Es una extensión básica que crea un elemento iframe en cualquier página. He agregado el detector de eventos onClick en mi código para activar el iFrame cada vez que se hace clic en el logotipo de la extensión. Pero por alguna razón, cada vez que abro un sitio nuevo, el iFrame se carga automáticamente. Se vuelve a cargar cuando presiono el ícono de la extensión de Chrome, pero no puedo hacer que deje de aparecer cada vez que se visita un nuevo sitio. Aquí está mi código. ¡Cualquier ayuda sería apreciada!
manifiesto.json
{ "name": "Sample Extension", "action": {}, "permissions": ["activeTab", "scripting","tabs","clipboardRead", "clipboardWrite"], "background": { "service_worker": "background.js" }, "version": "1", "manifest_version": 3, "content_scripts": [{ "js": ["contentscript.js"], "matches": ["<all_urls>"], "all_frames": true }], "web_accessible_resources": [ { "resources": [ "frame.html" ], "matches": ["<all_urls>"] } ] }contentscript.js
var extensionOrigin = 'chrome-extension://' + chrome.runtime.id; if (!location.ancestorOrigins.contains(extensionOrigin)) { var iframe = document.createElement('iframe'); iframe.src = chrome.runtime.getURL('frame.html'); iframe.style.cssText = 'position:fixed;top:25px ;left:25px;display:block;' + 'width:calc(100% - 50px);height:calc(100vh - 50px);z-index:9999; background: none; border: none; border-radius: 10px'; document.body.appendChild(iframe); }fondo.js
chrome.action.onClicked.addListener((tab,tabId) => { chrome.scripting.executeScript({ files: ["contentscript.js"], target: {tabId: tab.id} }) });marco.html
<style> html, body, iframe, h2 { margin: 0; border: 0; padding: 0; display: block; width: 100vw; height: 100vh; background: none; color: black; } h2 { height: 50px; font-size: 20px; } iframe { height: 100vh; } </style> <iframe src="https://www.youtube.com/"></iframe>