Get HTML element via aria label

后端 未结 3 1010
不思量自难忘°
不思量自难忘° 2021-02-07 02:48

I\'m making a small chrome extension and for it I need to grab a div from the DOM to manipulate. I get the DOM but I\'m having trouble grabbing the required d

3条回答
  •  春和景丽
    2021-02-07 03:17

    querySelector or querySelectorAll with an attribute selector should do it:

    // The first element that matches (or null if none do):
    var element = document.querySelector('[aria-label="Message Body"]');
    // A list of matching elements (empty if none do):
    var list = document.querySelectorAll('[aria-label="Message Body"]');
    

    Or if that ID is stable, simply:

    var element = document.getElementById(":ik");
    

    (Note that the d is lower case; you have it in upper case in your example.)

    Either way, make sure your code doesn't run until the page is loaded, by including this in your manifest:

    "run_at": "document_end"
    

    (A little) more in this answer, which references this Google documentation.

提交回复
热议问题