javascript - Younow onLoad (with greasemonkey) -
when loading younow website messagebox "start" pops twice. -how can fix this?
// ==userscript== // @name test // @include https://www.younow.com/* // @version 1 // @grant none // @require http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js // ==/userscript== $(document).ready(function() { alert("start"); });
the alert
acts way on website, maybe because of reload after call, prepend it's ok me.
$(document).ready(function() { $('body').prepend("toto"); // code here });
also don't need use ready function, greasmonkey start script @ right time.
but problem is:
- i suppose want stuff when ajax elements loaded. best way observe dom.
- since web site change current page using ajax requests on clicking ,
hashchange
event doesn't work, use trick listen page change.
with script can use alert
function:
// ==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 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 class exits: if($('.trending-now').length || $('.ynicon ynicon-chat').length || $('.trending_title').length || $('.trending-tags-list').length) { // disconnect observer: observer.disconnect(); // clear interval : clearinterval(intervalmutation); // call code: pageready(); } } function pageready() { // code here: alert('start'); }
Comments
Post a Comment