html 获取当前页面,从chrome扩展获取当前页面的源HTML

d3acb2a76c084f20c1667c4863cee194.png

一只萌萌小番薯

将脚本注入要从中获取源的页面并将其发送回弹出窗口....的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", ""]}popup.html

Injecting Script....
popup.jschrome.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 // 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 += '';            break;        case Node.COMMENT_NODE:            html += '';            break;        case Node.DOCUMENT_TYPE_NODE:            // (X)HTML documents are identified by public identifiers            html += " + 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)});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值