我试图在"控制栏中添加一个按钮"在Netflix上使用Greasemonkey。我已经成功地在头部添加了一个样式标签,并在主体上添加了一个脚本。由于Netflix使用AJAX来实现所有,我使用waitForKeyElements来调用函数来插入按钮。
我一直在使用Firebug来查看Netflix的HTML(相关代码如下所示)。我想为该按钮插入另一个div作为该部分的子项。该按钮适合音量按钮和播放器状态。
<section class="player-control-bar no-select" role="control-bar">
<div class="player-control-button player-play-pause pause icon-player-pause" tabindex="0" role="button" aria-label="Pause"></div>
<div class="player-control-button volume icon-player-volume-full icon-player-volume-3" tabindex="0" role="button" aria-label="Mute">
<div class="player-status">
&#13;
为了进行添加,waitForKeyElements函数接收jQuery选择器(selectorTxt =&#34; section.player-control-bar&#34;)和函数名称(actionFunction = addDiv),它们作为字符串传输。
将section对象存储到jNode后,调用addDiv函数插入按钮。然而,div(fiv)不会附加到该部分。
// ==UserScript==
// @name Netflix Ajax
// @namespace https://www.netflix.com/watch
// @include https://*.netflix.com/watch*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
// @version 1
// @grant none
// ==/UserScript==
waitForKeyElements (
"section.player-control-bar" // also tried: "div.player-status"
, addDiv
);
function waitForKeyElements (
selectorTxt, /* Required: The jQuery selector string that specifies the desired element(s). */
actionFunction, /* Required: The code to run when elements are found. It is passed a jNode to the matched element.*/
bWaitOnce, /* Optional: If false, will continue to scan for new elements even after the first match is found.*/
iframeSelector /* Optional: If set, identifies the iframe to search.*/
) {
var targetNodes, btargetsFound;
if (typeof iframeSelector == "undefined"){
targetNodes = $(selectorTxt);
}
else{
targetNodes = $(iframeSelector).contents ()
.find (selectorTxt);
}
if (targetNodes && targetNodes.length > 0) {
btargetsFound = true;
/*--- Found target node(s). Go through each and act if they
are new.
*/
targetNodes.each ( function () {
var jThis = $(this);
var alreadyFound = jThis.data ('alreadyFound') || false;
if (!alreadyFound) {
//--- Call the payload function.
var cancelFound = actionFunction (jThis);
if (cancelFound)
btargetsFound = false;
else
jThis.data ('alreadyFound', true);
}
} );
}
else {
btargetsFound = false;
}
//--- Get the timer-control variable for this selector.
var controlObj = waitForKeyElements.controlObj || {};
var controlKey = selectorTxt.replace (/[^\w]/g, "_");
var timeControl = controlObj [controlKey];
//--- Now set or clear the timer as appropriate.
if (btargetsFound && bWaitOnce && timeControl) {
//--- The only condition where we need to clear the timer.
clearInterval (timeControl);
delete controlObj [controlKey]
}
else {
//--- Set a timer, if needed.
if ( ! timeControl) {
timeControl = setInterval ( function () {
waitForKeyElements ( selectorTxt,
actionFunction,
bWaitOnce,
iframeSelector
);
},
300
);
controlObj [controlKey] = timeControl;
}
}
waitForKeyElements.controlObj = controlObj;
}
function addDiv (jNode) {
var fiv = document.createElement('div');
fiv.setAttribute("class", "player-control-button player-filter icon-player-play");
fiv.setAttribute("tabindex", "0");
fiv.setAttribute("role", "button");
fiv.setAttribute("aria-label", "Filter");
fiv.setAttribute("on-click", "myFunction()");
jNode.append(fiv);
//Other Attempts:
//fiv.insertBefore(jNode);
//jNode.prepend(fiv);
//jNode.appendChild(fiv);
//jNode.before(fiv);
//jNode.parent("section").appendChild(fiv);
}
&#13;
我测试完全相同的代码只是交换播放器状态类的部分,它完美地插入按钮的代码(只是在错误的地方)。从div标签,我尝试了fiv.insertBefore(jNode),jNode.before(fiv)和jNode.parent(&#34; section&#34;)。appendChild(fiv)。知道为什么jNode不会附加到该部分吗?