的alert
行为这样这个网站上,也许是因为在通话后重装的,但有前面加上它的确定我。
$(document).ready(function() {
$('body').prepend("toto"); // your code here
});
另外,您不需要使用ready函数,greasmonkey可以在正确的时间启动您的脚本。
但问题是:
- 我想你想在所有的AJAX元件装入做你的东西。所以最好的方法就是观察这个dom。
- 由于网站使用点击AJAX请求更改当前页面,并且
hashchange
事件不起作用,我使用一些技巧来聆听任何页面更改。
有了这个脚本,您可以使用alert
功能:
// ==UserScript==
// @name test
// @include https://www.younow.com/*
// @version 1
// @grant none
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js
// ==/UserScript==
var observer = null;
initObserver();
function initObserver()
{
observer = new MutationObserver(onMutation);
observer.observe(document,
{
childList: true, // report added/removed nodes
subtree: true, // observe any descendant elements
});
}
$(window).on('hashchange', function(e)
{
initObserver();
});
intervalMutation = setInterval(onMutation.bind(null, null), 1000);
function locationObserver()
{
var oldLocation = location.href;
setInterval(function() {
if(location.href != oldLocation) {
onMutation(null);
oldLocation = location.href
}
}, 1000); // check every second
}
locationObserver();
function onMutation(mutations)
{
// Check if this class exits:
if($('.trending-now').length ||
$('.ynicon ynicon-chat').length ||
$('.trending_title').length ||
$('.trending-tags-list').length)
{
// Disconnect the observer:
observer.disconnect();
// Clear the interval :
clearInterval(intervalMutation);
// Call your code:
pageReady();
}
}
function pageReady()
{
// your code here:
alert('start');
}