我在 Chrome 控制台中为 DOM 元素编写了非典型属性:
document.getElementById('some_id').atypical_property = 'some text'
之后,我尝试读取 content_script.js 中的 atropic_property:
alert(document.getElementById('some_id').atypical_property);
但是这是未定义的!好的。我尝试在 Chrome 控制台中读取 atropic_property,这是“一些文本”! list 中的 Run_at 更改不会更改结果。 我尝试使用 setTimeout,但许多计时器的延迟 500 毫秒的迭代不会改变结果 (typeof document.getElementById('some_id').atropic_property == "undefined")
以 Chrome 控制台为例。在网站上的真实脚本中设置了非典型属性,我希望阅读它。 我可以读取此属性吗?
提前致谢。
最佳答案
在嵌入页面和postMessage中使用创建脚本(此代码在content_script.js中):
window.addEventListener("message", function(event) {
if (event.source != window)
return;
if (event.data.type && (event.data.type == "FROM_PAGE")) {
//...
}
}
function _script_(){
function _tc_(sel){
return typeof sel != "undefined" && sel !=null ? sel.t_content : "no";
}
window.postMessage({
type: "FROM_PAGE",
type_of_wp: _tc_(document.querySelector(".team-0 .withTip.firearms")),
rank : _tc_(document.querySelector(".team-0 .withTip.sp.g1eb8.ic12453f2.d3710")),
clothes: _tc_(document.querySelector(".team-0 .withTip.armor-plating")),
house:_tc_(document.querySelector(".register.statistics img.withTip"))
}, "*");
}
var n = document.createElement('script');
n.innerHTML = _script_.toString() + " _script_();";
document.body.appendChild(n);
关于javascript - Chrome 内容脚本。访问 DOM 对象的非典型属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21227091/