2017-04-14 1 views
0

younow 웹 사이트를로드 할 때 "Start"메시지 상자가 두 번 나타납니다. 어떻게 해결할 수 있습니까?Younow onLoad (greasemonkey 포함)

// ==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"); 
}); 

답변

1

alert 행동이 어쩌면 때문에 호출 후 다시로드의 본 웹 사이트의 방법,하지만 앞에 추가로 나를 위해 괜찮습니다.

또한 준비 기능을 사용할 필요가 없습니다. greasmonkey 적절한 시점에 스크립트를 시작하십시오.

그러나 문제는 :

  1. 난 당신이 모든 아약스 요소를로드 할 때 물건을한다고 가정합니다. 그래서 그것을하는 가장 좋은 방법은 dom을 관찰하는 것입니다.
  2. 클릭하면 웹 사이트가 AJAX 요청을 사용하여 현재 페이지를 변경하고 hashchange 이벤트가 작동하지 않기 때문에 어떤 트릭을 사용하여 페이지 변경 내용을 수신합니다.

    // ==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'); 
    } 
    
    :
  3. 이 스크립트

당신은 alert 기능을 사용할 수 있습니다