2011-10-26 41 views
0

我寫了一個GreaseMonkey腳本來爲blogger.com的HTML視圖添加一個按鈕。我有警報到處,所以我知道一切都在運行。腳本觸發但沒有結果

奇怪的是,它發射兩次,每次都略有不同。 當我導航到blogger.com帖子編輯時,腳本首先在黑色頁面上觸發,並以正確的順序(包括「內部」順序)遍歷所有警報。

在頁面加載後的第二次運行中,除「內部」之外的每個警報都會運行並且不會產生錯誤。

// ==UserScript== 
// @name Blogger: Remove HTML Space 
// @include http://www.blogger.com/blogger.g?blogID*editor/target*\ 
// ==/UserScript= 

function contentEval(source) { 
    // Check for function input. 
    alert("asdas"); 
    if ('function' == typeof source) { 
    // Execute this function with no arguments, by adding parentheses. 
    // One set around the function, required for valid syntax, and a 
    // second empty set calls the surrounded function. 
    source = '(' + source + ')();' 
    } 

    alert("asdas2"); 
    // Create a script node holding this source code. 
    var script = document.createElement('script'); 
    alert("asdas3"); 
    script.setAttribute("type", "application/javascript"); 
    alert("asdas4"); 
    script.textContent = source; 
    alert("asdas5"); 

    // Insert the script node into the page, so it will run, and immediately 
    // remove it to clean up. 
    alert("asdas6"); 
    document.body.appendChild(script); 
    alert("asdas7"); 
} 

    alert("test") 
    contentEval(function(){ alert("inside"); document.getElementById("postingHtmlToolbar").firstChild.innerHTML += '<div id="SP" class="goog-inline-block goog-toolbar-button" title="SP" role="button" style="-moz-user-select: none;"><div class="goog-inline-block goog-toolbar-button-outer-box"><div class="goog-inline-block goog-toolbar-button-inner-box"><button type="button" onclick="document.getElementById(\'postingHtmlBox\').value = document.getElementById(\'postingHtmlBox\').value.replace(/&amp;nbsp;/g, \' \');"><b>SP</b></button></div></div></div>'}); 

alert("testw"); 

blogger.com是否有反腳本注入的東西?

回答

1

關於射擊兩次:

如果頁面包含 S,userscript將在他們每個人的火。

您需要確定您是否在之內。最簡單的方式火只在主網頁,而不是內部框架,是包裝所有代碼:

if(top==self) 
{ 
    ... 
} 

要查看是否有在頁面是<iframe>s(以HTML或者硬編碼或動態生成的),我一直用的AdBlock Plus(「打開可鎖定項目」對話框)。

但我不確定爲什麼第二個「內部」警報不會啓動。我創建了一個簡單的HTML頁面,包含,我得到兩個警報。

關於腳本一般:

你在這裏做重複工作。您不必包裝所有內容來執行腳本。 Greasemonkey在頁面加載後執行代碼行(在DOMContentLoaded上)。所以你不必創建<script>標籤,設置它的正文並附加到文檔中。你可以只寫

document.getElementById("postingHtmlToolbar").firstChild.innerHTML += ...

,它應該做你想做的(你不需要contentEval職能)。

我沒有博客上的博客,所以我不能記錄和看到任何URL和有問題的行爲,但與代碼一步一步播放應該足以進行調試。

編輯2:

看來你應該改變firstChildfirstElementChild

innerHTML財產Element(因爲火狐3.5),但不是在Node。 是的,它有時會非常不協調和煩人。我最近遇到了類似的問題。

編輯3:

有一對夫婦的事情出現。請不要告訴我這不還是工作:)

var func = function() { 
    var obj = document.getElementById("postingHtmlToolbar"); 
    if(obj !== null){ // only if object exists, so we don't have errors 
     //alert(obj); 
     obj.style.display = 'block'; // it's hidden by default 
     obj.firstElementChild.innerHTML += "FOO"; 
    } 
} 

window.setTimeout(func, 3000); // execute with delay -- let's give Google's JS time to be loaded first 
+0

有趣的是,如果我的腳本爲什麼沒有做它應該做的事情,我還是不知道。 總而言之,我不關心腳本最終是否會觸發100次。 您是否認爲問題可能只是由於多次運行而發生? – Jonathon

+0

我之前並沒有在腳本中詳細看過,請查看編輯內容,準確地說出發生了什麼以及發生了什麼。問題是追加HTML或點擊按鈕? 'document.getElementById(「postingHtmlToolbar」)。firstChild.innerHTML + =「FOO」'工作嗎? 關於多次發射,我認爲每次執行都在單獨的頁面上下文中,所以它們不應該互相影響(但我不完全確定)。 –

+0

@edit:我不認爲greasemonkey可以做這樣的事情,它不能直接編輯頁面。 不要問我爲什麼通用汽車公司不能以簡單的方式來運行你100%信任的代碼,但出於安全原因,我不明白他們限制你可以做的事情。 – Jonathon

0

GM可以直接修改網頁這是通用汽車

的那<button>標籤的用處......沒有你的意思是輸入?

我相信是執行兩次,因爲做這種方法,我猜是你的解決方法,不能夠直接修改頁面。

你正在做的是你正在創建一個命名匿名函數,如果你對JS不是很有經驗,那麼「哇」就是想出來。這將是問題:

if ('function' == typeof source) { 
    // Execute this function with no arguments, by adding parentheses. 
    // One set around the function, required for valid syntax, and a 
    // second empty set calls the surrounded function. 
    source = '(' + source + ')();' 
} 

當您設置之間的SOURCE + +您所呼叫的功能,因爲當你在串聯和功能是不是一個字符串,然後CONCAT正試圖將其轉換爲它會執行該函數,但最後empty()會再次執行,而不會在concat中執行。我沒有測試它,但我確實相信這一切正在發生。

所以jakub正確地指出你的代碼的複雜性是不必要的,最終會給你帶來錯誤。

相關問題