通过userscript删除网站标题的一部分

时间:2013-01-14 15:25:55

标签: javascript userscripts

我尝试编写一个userscript代码,用于删除网站标题(flash browsergame)中的所有内容,但会删除开始操作时显示的倒计时。

我是Javascript的新手,需要一些帮助。

更新

正则表达式问题已经解决,但我仍然需要一些帮助来让这个脚本“监控”标题,以便每当游戏发生变化时脚本再次运行。 < / p>

主标题如下:

  

Shakes&amp; Fidget - 游戏(buffed buffed)

一旦开始动作,倒计时就会添加到开头,所以标题会变为

  

02:26 - 摇晃&amp; Fidget - 游戏(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

它必须是正则表达式匹配,因为包含字符串“(buffed buffed)”会更改(它显示服务器名称)。

另一个问题是脚本应该“监视”标题,因为每次启动或完成新操作时它都会更改,但我的脚本只运行一次(没有正则表达式进行测试)。

先谢谢你的帮助,

slaecker

3 个答案:

答案 0 :(得分:2)

你的regexp文字存在一些问题。它必须被包装到/ -es,它会错过一个乘数,没有它,它只匹配一个字符,你需要使用“g”修饰符使其全局,以便匹配数字两边的字符串部分。

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

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

答案 1 :(得分:2)

对于正则表达式,请使用:

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

要检测标题更改,请使用MutationObservers,这是在Google Chrome和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()

答案 2 :(得分:1)

看起来您的计时器/计数器具有明确的格式。假设格式为##:##,您可以形成您的RegEx:

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

然后这将获得特定的计时器模式(而不是它在标题开头找到的任何数字和冒号)。

不要忘记用斜杠包装正则表达式。在正斜杠后使用g(如上所示)将在标题中找到其他计时器实例。如果您知道自己只有一个,那么最好将g放在最后。

相关问题