2013-05-27 34 views
1

我正在編寫一個腳本,它改變了我使用了很多網站的消息系統。它在每個消息線程的頂部添加一個按鈕。點擊後,它會自動移至線程中最新的消息。如何在執行php後獲取用戶腳本進行加載?

我遇到的問題是消息是由php加載和腳本在消息加載之前觸發。該站點也不會同時加載所有消息線程。它會加載10個左右,然後當您向下滾動時會加載更多。有沒有辦法讓按鈕加載每個消息線程,或者我是否爲無法達到的目標拍攝?

// ==UserScript== 
// @name   Imgur - Message Viewer 
// @namespace  Basion 
// @description It skips to the bottom of the most recent message 
// @author  Basion 
// @include  http://imgur.com/* 
// @include  https://imgur.com/* 
// @include  http://*.imgur.com/* 
// @include  https://*.imgur.com/* 
// @run-at  document-end 
// ==/UserScript== 
var messages = document.getElementsByClassName("thread-wrapper"); 

var newA = document.createElement('a'); 

for (var i = 0; i < messages.length; i++) { 
    newA.addEventListener("click", scrollToMessage(i), true); 
    newA.innerText = "Go to Newest Message"; 
    messages[i].appendChild(newA); 
} 
function scrollToMessage(id) { 
    var newMessages = document.getElementsByClassName('thread-wrapper'); 
    newMessages[id].scrollIntoView(false); 
} 
+0

你能從你添加一些源代碼? – reporter

+1

我們可以看看您實際上想要使用該腳本的網站嗎?新內容如何加載?您可以考慮在執行前等待1秒鐘。或者,也許在點擊一個按鈕之後,腳本每秒鐘計算一次線程數。如果線程數量增加(即成功加載),則滾動至該值。 – kevinamadeus

+0

@kevinamadeus我想在[Imgur](http://www.imgur.com)上使用它您必須創建一個帳戶才能看到消息系統。我相信你也可以發送消息給自己,如果這能幫助你理清一點。 – user2424715

回答

1

爲了處理通過AJAX添加元素,使用jQuerywaitForKeyElements如圖"Run Greasemonkey script on the same page, multiple times?"
Here's a handy jQuery reference

此外,你不能以這種方式發送id到事件監聽器。

這裏的一個完整的工作,後跳腳本,適應問題的代碼和Imgur目標頁面(S):

// ==UserScript== 
// @name   Imgur - Message Viewer 
// @namespace  Basion 
// @description It skips to the bottom of the most recent message 
// @author  Basion 
// @include  http://imgur.com/* 
// @include  https://imgur.com/* 
// @include  http://*.imgur.com/* 
// @include  https://*.imgur.com/* 
// @run-at  document-end 
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js 
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js 
// @grant   GM_addStyle 
// ==/UserScript== 
/*- The @grant directive is needed to work around a design change 
    introduced in GM 1.0. It restores the sandbox. 
*/ 

waitForKeyElements ("div.thread-wrapper", addJumpButton); 

/*-- Prepend a button to the top of each thread. Don't add in the header, to 
    avoid conflicts with the open/close toggle. 
*/ 
function addJumpButton (jNode) { 
    jNode.prepend (
     '<a href="#" class="gmThreadJmpBtn">Go to Newest Message in thread</a>' 
    ); 
} 

//-- Activate *all* the jump buttons with one jQuery function. 
$("div.notifications").on (
    "click", "div.thread-wrapper a.gmThreadJmpBtn", jumpToEndOfThread 
); 

function jumpToEndOfThread (zEvent) { 
    //-- The last (newest) message will be just above the "reply bar". 
    var thisThreadHdr = $(zEvent.target); 
    var targetDiv  = thisThreadHdr.nextAll ("div.reply.striped"); 
    targetDiv[0].scrollIntoView (false); 

    //-- Block the page handlers from also firing for this link. 
    zEvent.stopPropagation(); 
    zEvent.preventDefault(); 
} 
+0

這很好。 我想我沒有想太多的開放/關閉的事情。我想我也應該更多地關注jquery。正如你所想象的,我對這一切都很陌生。 謝謝! – user2424715

+0

不客氣。您仍然可以將鏈接添加到標題,但它使事情變得複雜。我展示了更簡單/更簡單的答案。 –