0
我正在學習如何製作網絡附加信息,並認爲通過製作喜歡YouTube視頻的附加信息開始會很有趣。如何讓內容腳本點擊YouTube視頻的LIKE按鈕?
我的清單部分與YouTube互動如下:
"content_scripts": [
{
"run_at": "document_end",
"matches": ["*://*.youtube.com/*"],
"js": ["/content_scripts/youtube.js"]
}
],
我的代碼如下:
let likeButton = document.querySelector('button[title="I like this"]');
if (likeButton != null) {
likeButton.click();
console.log(likeButton);
console.log("clicked on like button");
}
在我的控制檯我可以看到日誌「點擊喜歡按鈕」。我也可以看到按鈕本身,但「點擊」部分永遠不會通過,或者至少它不會「喜歡」視頻。
如果我檢查loged「likeButton」,我可以看到這一點:
<button class="yt-uix-button yt-uix-button-size-default yt-uix-button-opacity yt-uix-button-has-icon no-icon-markup like-button-renderer-like-button like-button-renderer-like-button-unclicked yt-uix-post-anchor yt-uix-tooltip" type="button" onclick=";return false;" title="I like this" aria-label="like this video along with 637 other people" data-post-action="/service_ajax" data-force-position="true" data-orientation="vertical" data-position="bottomright" data-post-data="se=8pH38AERCAASDQoLeHRRQ1FZbEdTem8%3D&itct=CDQQpUEiEwi0iLq78sLTAhUHz5AKHQxkD1wo-B0" data-tooltip-text="I like this" aria-labelledby="yt-uix-tooltip166-arialabel">
如果我把在谷歌瀏覽器的控制檯變量,把它放在一個變量,「點擊」它與按鈕.click();它的工作原理。但內容腳本沒有,我不知道爲什麼。
你可以交替使用YouTube API:https://developers.google.com/youtube/v3/docs/videos/rate –