ver una pagina modificar google fuente extraer descargar como codigo chrome html google-chrome-extension

html - una - ver codigo fuente chrome android



Obtener el código fuente HTML de la página actual desde la extensión de Chrome (1)

Tengo una extensión de cromo. Necesito analizar desde la fuente HTML de la página actual. Encontré aquí todo tipo de soluciones con páginas de fondo y scripts de contenido, pero ninguno me ayudó. aquí es lo que tengo hasta ahora:
manifest.json:

{ "name": "Extension", "version": "1.0", "description": "Extension", "browser_action": { "default_icon": "bmarkred.ico", "popup": "Test.html" }, "content_scripts": [ { "matches": ["http://*/*"], "js": ["content.js"] } ], "background": { "page": "backgroundPage.html" }, "permissions": [ "cookies", "tabs", "http://*/*", "https://*/*" ] }

background.html:

<html> <head> <script type="text/javascript"> try { chrome.tabs.getSelected(null, function (tab) { chrome.tabs.sendRequest(tab.id, {action: "getSource"}, function(source) { alert(source); }); }); } catch (ex) { alert(ex); } </script> </head> </html>

content.js:

chrome.extension.onRequest.addListener(function(request, sender, callback) { if (request.action == "getSource") { callback(document.getElementsByTagName(''html'')[0].innerHTML); } });

La alerta siempre alerta indefinida. incluso si cambio en el archivo content.js la función de devolución de llamada a:

callback(''hello'');

sigue siendo el mismo resultado. ¿Qué estoy haciendo mal? tal vez estoy yendo en esto de la manera incorrecta. lo que realmente necesito es esto. Cuando el usuario abre la ventana emergente de extensión (y solo entonces), necesito HTML de la página actual para poder analizarlo. ¿alguna sugerencia?


Inyecte una secuencia de comandos en la página de la que desea obtener la fuente y envíela de nuevo al mensaje emergente ....

manifest.json

{ "name": "Get pages source", "version": "1.0", "manifest_version": 2, "description": "Get pages source from a popup", "browser_action": { "default_icon": "icon.png", "default_popup": "popup.html" }, "permissions": ["tabs", "<all_urls>"] }

popup.html

<!DOCTYPE html> <html style=''''> <head> <script src=''popup.js''></script> </head> <body style="width:400px;"> <div id=''message''>Injecting Script....</div> </body> </html>

popup.js

chrome.runtime.onMessage.addListener(function(request, sender) { if (request.action == "getSource") { message.innerText = request.source; } }); function onWindowLoad() { var message = document.querySelector(''#message''); chrome.tabs.executeScript(null, { file: "getPagesSource.js" }, function() { // If you try and inject into an extensions page or the webstore/NTP you''ll get an error if (chrome.runtime.lastError) { message.innerText = ''There was an error injecting script : /n'' + chrome.runtime.lastError.message; } }); } window.onload = onWindowLoad;

getPagesSource.js

// @author Rob W <http://.com/users/938089/rob-w> // Demo: var serialized_html = DOMtoString(document); function DOMtoString(document_root) { var html = '''', node = document_root.firstChild; while (node) { switch (node.nodeType) { case Node.ELEMENT_NODE: html += node.outerHTML; break; case Node.TEXT_NODE: html += node.nodeValue; break; case Node.CDATA_SECTION_NODE: html += ''<![CDATA['' + node.nodeValue + '']]>''; break; case Node.COMMENT_NODE: html += ''<!--'' + node.nodeValue + ''-->''; break; case Node.DOCUMENT_TYPE_NODE: // (X)HTML documents are identified by public identifiers html += "<!DOCTYPE " + node.name + (node.publicId ? '' PUBLIC "'' + node.publicId + ''"'' : '''') + (!node.publicId && node.systemId ? '' SYSTEM'' : '''') + (node.systemId ? '' "'' + node.systemId + ''"'' : '''') + ''>/n''; break; } node = node.nextSibling; } return html; } chrome.runtime.sendMessage({ action: "getSource", source: DOMtoString(document) });