【发布时间】:2019-03-30 01:48:53
【问题描述】:
我有一个将js 代码注入 YouTube 页面的扩展程序。我在manifest.json 中使用了以下声明:
"content_scripts": [
{
"matches": [
"*://*.youtube.com/*"
],
"js": [
"background.js"
]
}
]
我想定义当我转到另一个视频时打印视频名称、喜欢和不喜欢的数量到控制台的函数。
我写在background.js:
window.onhashchange = function () {
console.log(
document.querySelector("h1.title > yt-formatted-string:nth-child(1)").innerHTML, "\n",
document.querySelector("ytd-toggle-button-renderer.ytd-menu-renderer:nth-child(1) > a:nth-child(1) > yt-formatted-string:nth-child(2)").getAttribute("aria-label"), "\n",
document.querySelector("ytd-toggle-button-renderer.style-scope:nth-child(2) > a:nth-child(1) > yt-formatted-string:nth-child(2)").getAttribute("aria-label"), "\n",
)
}
但它只运行一次。如果我从“推荐”中选择新视频,它将不起作用。我也试过.onload、.onunload等
UPD:现在我发现的唯一方法是使用.setInterval。
【问题讨论】:
标签: javascript youtube firefox-addon-webextensions