简体   繁体   English

如何使用chrome.tabs.create从新创建的chrome选项卡中获取元素文本

[英]How to get an element text from a newly created chrome tab using chrome.tabs.create

Im very new in creating a chrome extension. 我在创建chrome扩展程序方面非常新。

I have created few tabs using the code: 我使用以下代码创建了一些标签:

chrome.tabs.create({url:" https://www.example.com/user.html#e "+userId, active:true}, function(tab){} chrome.tabs.create({url:“ https://www.example.com/user.html#e ” + userId,active:true},function(tab){}

Post creation how can I access the DOM and elements inside the newly created tabs. 创建后,如何访问新创建的选项卡中的DOM和元素。

Please help me. 请帮我。

Content scripts 内容脚本

Chrome extensions can access the DOM via content scripts. Chrome扩展程序可以通过内容脚本访问DOM

As the docs explain, these scripts can be loaded into tabs and then access the DOM from there. 如文档所述,可以将这些脚本加载到选项卡中,然后从那里访问DOM。 For example: 例如:

manifest.json: manifest.json:

{
  "version": "0.1.0",
  "name": "Hello world",
  "description": "Sample extension using content script",
  "permissions": [
    "<all_urls>",
    "tabs"
  ],
  "content_scripts": [
    {
      "matches": [
        "http://*/*",
        "https://*/*"
      ],
      "js": [
        "contentScript.js"
      ],
      "run_at": "document_end"
    }
  ],
  "manifest_version": 2
}

contentScript.js: contentScript.js:

var myElement = document.getElementById("mySelect");

// Logic to alter myElement, etc.

If you want to send messages and data between the content script and the background of your extension then you'll need to take a look at message passing . 如果您要在内容脚本和扩展程序的后台之间发送消息和数据,那么您需要看一下消息传递

chrome.tabs.executeScript chrome.tabs.executeScript

You can also access the DOM using the chrome.tabs.executeScript method. 您还可以使用chrome.tabs.executeScript方法访问DOM。

This SO answer explains how. 这样的答案解释了如何。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM