如何获取 DOM 元素的 ::before 内容为 JavaScript?

How to get a DOM element's ::before content with JavaScript?

我想知道是否可以获取 DOM 元素的 ::before 内容,该内容由 CSS3 设置。

我尝试了一些方法,还是做不到,所以很迷茫!

// https://rollbar.com/docs/

const links = document.querySelectorAll(`ul.image-list a`);

links[0];
// <a href="/docs/notifier/rollbar-gem/" class="ruby">::before Ruby</a>

links[0];
//

links[0].textContent;
//"Ruby"

links[0].innerText;
// "Ruby"

links[0].innerHTML;
// "Ruby"

// ??? links[0]::before;

是这样的:

":before"作为第二个参数传递给window.getComputedStyle():

console.log(getComputedStyle(document.querySelector('p'), ':before').getPropertyValue('content'));
p::before,
p::after {
  content: ' Test ';
}
<p>Lorem Ipsum</p>

getComputedStyle() & getPropertyValue()

getComputedStyle(document.querySelector('span.search-box'), '::after').getPropertyValue('content');