如何在 JS 中获取未知 id 和 类 的值和 textContents

How to get values and textContents of unknown ids and classes in JS

假设我面前的网站上有一些文字或数字。我想制作一个程序,将那些特定的 words/numbers 更改为其他内容。目前,我打算使用 google 扩展并将一些 JS 编码为内容脚本来执行此操作。这么久以来,我一直在想办法。我希望它是通用的并且可以在多个网站上工作,因为我不能硬编码指向每个网站的值(我需要的)的 id 和 类。

我试过 querySelectorAll、document.someMethod,甚至 window.find() 都无济于事。

以下是我的一些其他想法,可能有助于您回答我的问题。

有没有办法从网站上获取HTML代码并将其变成一个数组,循环遍历文档并使用if语句找到我需要的具体单词和数字,然后最后更改它们使用我的 JS 代码?从 HTML 中取出所有 ID 和 类 并检查它是否与我想要的特定值有任何关系怎么样?

main.js(内容脚本)

// How do I generalize querySelectorAll to take every instance of IDS or class names that follow a specific pattern? Lets say I wanted to change the values of each element in the HTML code that had IDs related to the string "titles". 
ids = document.querySelectorAll('')

如果您需要澄清或详细信息,请询问,我会立即提供。

所以这个 chrome 扩展的代码库应该能够帮助您走上正确的轨道:https://github.com/mark-monteiro/drumpfinator/blob/master/src/inject/inject.js

它基本上完成了您想要做的事情。它采用单词 Donald Trump via regex 的所有实例,并将其替换为一些随机有趣的形容词。

根据该示例,您可以使用正则表达式或某些数组来找出您需要的词,并将它们替换为您喜欢的词,也许使用键值对象。