栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Chrome扩展程序-获取DOM内容

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Chrome扩展程序-获取DOM内容

术语“背景页面”,“弹出窗口”,“内容脚本”仍然让您感到困惑;我强烈建议您更深入地了解GoogleChrome扩展程序文档

关于您的问题,内容脚本或后台页面是不是要走的路:

内容脚本 :绝对地, 内容脚本是扩展程序中有权访问网页DOM的唯一组件。

后台页面/弹出窗口 :也许(可能最多两个)之一,您可能需要让内容脚本将DOM内容传递给后台页面或弹出窗口以进行进一步处理。


让我重复一遍,我强烈建议您对可用文档进行更仔细的研究!就是说,这里是一个示例扩展,它检索StackOverflow页面上的DOM内容并将其发送到后台页面,然后在控制台中将其打印出来:

background.js:

// Regex-pattern to check URLs against. // It matches URLs like: http[s]://[...]stackoverflow.com[...]var urlRegex = /^https?://(?:[^./?#]+.)?stackoverflow.com/;// A function to use as callbackfunction doStuffWithDom(domContent) {    console.log('I received the following DOM content:n' + domContent);}// When the browser-action button is clicked...chrome.browserAction.onClicked.addListener(function (tab) {    // ...check the URL of the active tab against our pattern and...    if (urlRegex.test(tab.url)) {        // ...if it matches, send a message specifying a callback too        chrome.tabs.sendMessage(tab.id, {text: 'report_back'}, doStuffWithDom);    }});

content.js:

// Listen for messageschrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {    // If the received message has the expected format...    if (msg.text === 'report_back') {        // Call the specified callback, passing        // the web-page's DOM content as argument        sendResponse(document.all[0].outerHTML);    }});

manifest.json:

{  "manifest_version": 2,  "name": "Test Extension",  "version": "0.0",  ...  "background": {    "persistent": false,    "scripts": ["background.js"]  },  "content_scripts": [{    "matches": ["*://*.stackoverflow.com/*"],    "js": ["content.js"]  }],  "browser_action": {    "default_title": "Test Extension"  },  "permissions": ["activeTab"]}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/575136.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号