我已经修改了一个现有的脚本来创建一个按钮,用于在youtube上隐藏/显示相关的侧边栏。默认情况下,应隐藏相关部分。类似于FF插件YT Clean。
如果“related”块在默认情况下是可见的(代码中没有button.click() ),则该按钮工作得很好。如果我重新加载...watch页面,也同样有效。
但如果我来自默认的youtube页面或搜索结果(点击视频观看),我必须点击两次按钮来隐藏“相关”区块。
我真的是一个初学者(我没有写这篇文章),所以感谢任何帮助。
// ==UserScript==
// @name Youtube - toggle related
// @namespace -
// @version 1.0
// @match https://www.youtube.com/*
// @grant none
// @require https://code.jquery.com/jquery-3.3.1.min.js
// @run-at document-end
// @noframes
// ==/UserScript==
(function(){
'use strict';
console.log('X');
let target = document.querySelectorAll('body')[0];
let options = {'childList': true, 'subtree' : true};
function callback(observer, node){
if (node.nodeName.toLowerCase() == '#secondary-inner'){
$('#secondary-inner').hide();
}
}
let observer = new MutationObserver(function(mutations){
mutations.forEach(function(mutation){
if (mutation.type === 'childList'){
mutation.addedNodes.forEach(function(node){
callback(observer, node);
});
}
});
});
let button = document.createElement('button');
button.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="48" height="24" viewBox="0 0 24 24"><g fill="currentColor"><path d="M4 14h4v-4H4v4zm0 5h4v-4H4v4zM4 9h4V5H4v4zm5 5h12v-4H9v4zm0 5h12v-4H9v4zM9 5v4h12V5H9z"/></g></svg>';
button.style = `
background: transparent;
border: 0;
color: rgb(96,96,96);
cursor: pointer;
outline: 0;
`;
let hide = false;
button.onclick = ()=>{
hide = !hide;
if (hide){
if ($('#secondary-inner')[0])
$('#secondary-inner').hide()
else
observer.observe(target, options);
console.log(`hide`);
}
else{
observer.disconnect();
console.log(`show`);
$('#secondary-inner').show();
}
}
button.click();
let menu = $('#end')[0];
menu.insertBefore(button, menu.lastElementChild);
console.log('inserted');
} )()
发布于 2021-07-02 07:09:23
如果您想在默认情况下始终隐藏元素而不显示任何按钮,您可以这样做:
// ==UserScript==
// @name Youtube - Hide Related Sidebar
// @namespace -
// @version 1.0
// @match https://www.youtube.com/*
// @grant none
// @require https://code.jquery.com/jquery-3.6.0.min.js
// @run-at document-end
// @noframes
// ==/UserScript==
(function(){
'use strict';
const callback = () => $('#secondary-inner').hide();
const observer = new MutationObserver(function(mutations){
mutations.forEach(function(mutation){
if (mutation.type === 'childList'){
mutation.addedNodes.forEach(function(node){
callback();
});
}
});
});
observer.observe(document.body, {'childList': true, 'subtree' : true});
})()
发布于 2021-07-02 18:15:49
这是一个工作版本。欢迎任何意见/修订。
// ==UserScript==
// @name Youtube - Toggle Related Sidebar
// @namespace -
// @version 1.2
// @match https://www.youtube.com/*
// @include https://www.youtube.com/watch*
// @grant none
// @require https://code.jquery.com/jquery-3.6.0.min.js
// @run-at document-end
// @noframes
// ==/UserScript==
(function(){
'use strict';
let target = document.querySelectorAll('body')[0];
let options = {'childList': true, 'subtree' : true};
const callback = () => $('#related').hide();
const observer = new MutationObserver(function(mutations){
mutations.forEach(function(mutation){
if (mutation.type === 'childList'){
mutation.addedNodes.forEach(function(node){
callback();
});
}
});
});
observer.observe(document.body, {'childList': true, 'subtree' : true});
let button = document.createElement('button');
button.innerHTML = '<svg width="48" height="24" viewBox="0 0 24 24"><g fill="currentColor"><path d="M4 14h4v-4H4v4zm0 5h4v-4H4v4zM4 9h4V5H4v4zm5 5h12v-4H9v4zm0 5h12v-4H9v4zM9 5v4h12V5H9z"/></g></svg>';
button.style = ` background: transparent; border: 0; color: rgb(96,96,96); outline: 0; `;
let hide = true;
button.onclick = ()=>{
hide = !hide;
if (hide){
if ($('#related')[0])
$('#related').hide()
else
observer.observe(target, options);
console.log(`hide`);
}
else{
observer.disconnect();
console.log(`show`);
$('#related').show();
}
}
let menu = $('#end')[0];
menu.insertBefore(button, menu.lastElementChild);
console.log('inserted');
} )()
https://stackoverflow.com/questions/68137471
复制相似问题