如何在 yt-navigate-finish(chrome 扩展名)后获取文档?
How can i get document after yt-navigate-finish (chrome extension)?
我对非常有趣的事情有很大的疑问。实际上我有一个代码,当我想要 getAttribute
时,我总是得到相同的值(例如:第一个 content="1234123123"
,更改视频内容后应该更改,但我又得到了 content="1234123123"
), 但 window.location.href
returns 通常。
刷新网站正常,点击其他视频就不行了。有人帮帮我吗?
Javascript
window.addEventListener("yt-navigate-finish", function(event) {
console.log("================== ")
console.log($('body meta[itemprop=channelId]').attr("content"))
console.log(window.location.href)
let x = document.querySelector("#watch7-content meta[itemprop=channelId]")
console.log(x.getAttribute("content"))
let x2 = window.document.querySelector("#watch7-content meta[itemprop=channelId]")
console.log(x2.getAttribute("content"))
});
Manifest.json
"content_scripts": [
{
"run_at": "document_start",
"matches": ["https://www.youtube.com/watch?v=*"],
"js": [
"lib/jquery-3.6.0.min.js",
"initVideo.js"
]
},
你的问题有点令人困惑:)
据我了解,当您导航到 YouTube 上的新视频时,您希望获得频道 ID。
如果是这样,此处使用的查询 document.querySelector("#watch7-content meta[itemprop=channelId]")
不会在您每次切换视频时改变。
这将:
document.addEventListener("yt-navigate-finish", function (event) {
// The query is long, but it's not random, it make sense.
const channelElement = document.querySelector("#primary-inner #meta .ytd-video-owner-renderer a");
if (channelElement.href)
console.log(channelElement.href.split("channel/")[1]);
});
告诉我是否有帮助,或者您还有其他意思:)
我对非常有趣的事情有很大的疑问。实际上我有一个代码,当我想要 getAttribute
时,我总是得到相同的值(例如:第一个 content="1234123123"
,更改视频内容后应该更改,但我又得到了 content="1234123123"
), 但 window.location.href
returns 通常。
刷新网站正常,点击其他视频就不行了。有人帮帮我吗?
Javascript
window.addEventListener("yt-navigate-finish", function(event) {
console.log("================== ")
console.log($('body meta[itemprop=channelId]').attr("content"))
console.log(window.location.href)
let x = document.querySelector("#watch7-content meta[itemprop=channelId]")
console.log(x.getAttribute("content"))
let x2 = window.document.querySelector("#watch7-content meta[itemprop=channelId]")
console.log(x2.getAttribute("content"))
});
Manifest.json
"content_scripts": [
{
"run_at": "document_start",
"matches": ["https://www.youtube.com/watch?v=*"],
"js": [
"lib/jquery-3.6.0.min.js",
"initVideo.js"
]
},
你的问题有点令人困惑:)
据我了解,当您导航到 YouTube 上的新视频时,您希望获得频道 ID。
如果是这样,此处使用的查询 document.querySelector("#watch7-content meta[itemprop=channelId]")
不会在您每次切换视频时改变。
这将:
document.addEventListener("yt-navigate-finish", function (event) {
// The query is long, but it's not random, it make sense.
const channelElement = document.querySelector("#primary-inner #meta .ytd-video-owner-renderer a");
if (channelElement.href)
console.log(channelElement.href.split("channel/")[1]);
});
告诉我是否有帮助,或者您还有其他意思:)