简体   繁体   中英

Google Chrome Extension - Get Tab Contents in a String

I am creating a Google Chrome extension and I need to import the page HTML into my extension as a string so that it can be used.

I have tried:

document.getElementById('container');

But I get a "null" in return. I have made sure that it is the correct div id.

Is there something wrong with the code I am using or does this method simply not work?

To get your container div as String you shoud use innerText or innerHTML .

As Follows :

var asText = document.getElementById('container').innerText;
var asHTML = document.getElementById('container').innerHTML;

If you get a null answer without using any methods it means either your div doesn't exist or not is loaded.

Edit :

Don't forget to specify in the manifest.json file at which time your javascript should be executed. You can do this by using the run_at parameter.

manifest.json

{
      "name": "The name of your extension",
      "version": "0.0.1",
      "manifest_version": 2,
      "description": "Your description goes here",
          "homepage_url": "http://example.com/",  
      "content_scripts": [
            {
                  "run_at": "document_end",
                  "all_frames": false,
                  "matches": [
                        "*"
                  ],
                  "js": [
                        "*"
                  ]
            }
      ],
      "web_accessible_resources": [
            "*"
      ],
      "permissions": [
            "storage"
      ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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