2013-01-14 18 views
1

我嘗試編寫一個userscript,消除從網站(閃光網頁遊戲)的稱號一切,但在開始行動時出現的倒計時刪除網站的標題的一部分。通過userscript

我新的Javascript和需要一些幫助。

UPDATE

的正則表達式問題解決,但我還是需要一些幫助,讓這個腳本來「監視」的稱號因此每次它由腳本再次運行遊戲的變化。一旦

Shakes & Fidget - The Game (buffed buffed)

爲動作開始倒計時被添加到開始這樣的標題更改爲

02:26 - Shakes & Fidget - The Game (buffed buffed)

我想要的標題:

的主標題是這樣的只顯示倒計時。

我搜索網,發現不同的方式來做到這一點,但他們沒有爲我工作。

這是我目前有:

// ==UserScript== 
// @name  Shakes & Fidget Buffed title shortener 
// @namespace http://släcker.de 
// @version 0.1 
// @description Removes the page title of Shakes & Fidget to only display left time if it exists 
// @include  *.sfgame.* 
// @exclude  www.sfgame.* 
// @exclude  sfgame.* 
// @copyright 2013+, slaecker 
// ==/UserScript== 

var regex = [^0-9:] 

function cleanTitle() { 
    var oldTitle = document.title; 
    var oldTitleRX = oldTitle.match(regex); 
    document.title = oldTitle.replace(oldTitleRX,""); 
    return oldTitle; 
} 


cleanTitle() 

JavaScript控制檯顯示有關正則表達式的錯誤。我試圖逃跑的人物,但錯誤是一樣的:

env: ERROR: Syntax error @ 'Shakes & Fidget Buffed title shortener'! 
Unexpected token^
SyntaxError: Unexpected token^
    at Window.Function (<anonymous>) 
    at L (eval at <anonymous> (eval at <anonymous> (chrome-extension://dhdgffkkebhmkfjojejmpbldmpobfkfo/content.js:51:21)), <anonymous>:156:21) 
    at n (eval at <anonymous> (eval at <anonymous> (chrome-extension://dhdgffkkebhmkfjojejmpbldmpobfkfo/content.js:51:21)), <anonymous>:384:2) 
    at R (eval at <anonymous> (eval at <anonymous> (chrome-extension://dhdgffkkebhmkfjojejmpbldmpobfkfo/content.js:51:21)), <anonymous>:388:86) 
    at Q (eval at <anonymous> (eval at <anonymous> (chrome-extension://dhdgffkkebhmkfjojejmpbldmpobfkfo/content.js:51:21)), <anonymous>:194:40) 
Uncaught SyntaxError: Unexpected token^
L 
n 
R 
Q 

它必須是一個正則表達式匹配,因爲包含字符串「(磨光磨光)」改變(它顯示了服務器名稱)。

的另一個問題是,腳本應「監視」的稱號,因爲它改變了每次新動作開始或完成了,但我的腳本只運行一次(測試它沒有正則表達式)。

預先感謝您的幫助,

slaecker

+3

var regex =/\ D/ – primetwig

+0

@SunnyTAR:這讓我在js控制檯中出現「SyntaxError:Unexpected token {」。據我所知,這不會在分鐘和秒之間離開「:」,對吧? –

回答

2

對於正則表達式,可以使用:

document.title = document.title.replace (/[^0-9:]/g, ""); 

要檢測標題更改,請使用MutationObservers,即在谷歌瀏覽器和Firefox(兩個主要瀏覽器)實現了一個新的HTML5功能。

完整的腳本將工作:

// ==UserScript== 
// @name  Shakes & Fidget Buffed title shortener 
// @namespace http://släcker.de 
// @version  0.1 
// @description Removes the page title of Shakes & Fidget to only display left time if it exists 
// @include  *.sfgame.* 
// @exclude  www.sfgame.* 
// @exclude  sfgame.* 
// @copyright 2013+, slaecker, Stack Overflow 
// @grant  GM_addStyle 
// ==/UserScript== 
/*- The @grant directive is needed to work around a design change 
    introduced in GM 1.0. It restores the sandbox. 
*/ 

var MutationObserver = window.MutationObserver || window.WebKitMutationObserver; 
var myObserver  = new MutationObserver (titleChangeDetector); 
var obsConfig  = { 
    //-- Subtree needed. 
    childList: true, characterData: true, subtree: true 
}; 

myObserver.observe (document, obsConfig); 

function titleChangeHandler() { 
    this.weInitiatedChange  = this.weInitiatedChange || false; 
    if (this.weInitiatedChange) { 
     this.weInitiatedChange = false; 
     //-- No further action needed 
    } 
    else { 
     this.weInitiatedChange = true; 
     document.title = document.title.replace (/[^0-9:]/g, ""); 
    } 
} 

function titleChangeDetector (mutationRecords) { 

    mutationRecords.forEach (function (mutation) { 
     //-- Sensible, Firefox 
     if ( mutation.type      == "childList" 
      && mutation.target.nodeName   == "TITLE" 
     ) { 
      titleChangeHandler(); 
     } 
     //-- WTF, Chrome 
     else if (mutation.type      == "characterData" 
      && mutation.target.parentNode.nodeName == "TITLE" 
     ) { 
      titleChangeHandler(); 
     } 
    }); 
} 

//-- Probably best to wait for first title change, but uncomment the next line if desired. 
//titleChangeHandler(); 

如果您使用的是一些其他的瀏覽器(狀態,在這個問題),然後還原到使用setInterval()

+0

非常感謝,取消註釋「// titleChangeHandler();」它的作用就像一種魅力。我認爲這將是學習有關腳本創建的簡單任務,但似乎我必須學習更多。下一步將是像http://userscripts.org/scripts/show/24430這樣的favicon疊加倒計時。希望我會弄清楚如何做到這一點。 –

2

你有幾個問題,你的正則表達式文字。它必須被包裝到/ -es,它會丟失一個乘數,沒有它就只能匹配一個字符,並且需要使用「g」修飾符使其成爲全局的,以匹配數字兩側的字符串部分。

var regex = /[^0-9:]+/g; 

function cleanTitle() { 
    document.title = document.title.replace(regex, ""); 
} 
+0

謝謝,現在它可以工作一次,但一旦標題被遊戲改變,腳本將不會再次運行。我怎樣才能讓它「監視」這個標題,並在遊戲發生變化時改變它? –

+0

你在這裏混合了一些東西......上面是「清理」瀏覽器窗口的標題,只留下數字和冒號。現在,如果您想在某個時刻恢復原始標題,則需要將其保存在'.replace()'之前,以便可以替換原始值。如果你想隨着遊戲的進展更新數字,你需要實現一種方式,以便遊戲定期在你的頁面調用JS函數,例如將新的計數器值作爲參數傳遞給該函數,以便每次調用時都可以替換數字。 – marekful

1

它看起來像你的定時器/計數器有一定的格式。假定該格式是##:##,你可以由此形成您正則表達式:

var regex = /\d\d:\d\d/g; 

那麼這將讓特定的定時器模式(而非任何數字和冒號發現在節目的開頭)。

不要忘了你的包裹正則表達式語句斜線。正斜槓(如上所示)後使用g將在標題中找到其他定時器實例。如果你知道你只有一個,最好是把g關閉。