【问题标题】:Remove part of title of website via userscript通过用户脚本删除网站标题的一部分
【发布时间】:2013-01-14 15:25:55
【问题描述】:

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

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

更新

正则表达式问题已解决,但我仍然需要一些帮助才能让此脚本“监控”标题,以便每次游戏更改时脚本都会再次运行。

主标题如下所示:

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

它必须是一个正则表达式匹配,因为包含的字符串“(buffed buffed)”发生了变化(它显示了服务器名称)。

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

提前感谢您的帮助,

懒鬼

【问题讨论】:

  • @SunnyTAR:这在 js 控制台中给了我“SyntaxError: Unexpected token {”。据我所知,这不会在分钟和秒之间留下“:”,对吧?

标签: javascript userscripts


【解决方案1】:

对于正则表达式,使用:

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()

【讨论】:

  • 非常感谢,取消注释 "//titleChangeHandler ();"它就像一个魅力。我认为这将是一个简单的任务来了解用户脚本的创建,但似乎我必须学习更多。下一步是将倒计时作为像userscripts.org/scripts/show/24430 这样的图标覆盖。希望我能弄清楚如何做到这一点。
【解决方案2】:

您的正则表达式文字存在一些问题。它必须被包裹到 /-es,它错过了一个乘数,没有它它只匹配一个字符,并且您需要使用“g”修饰符使其全局化,以便匹配数字两侧的字符串部分。

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

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

【讨论】:

  • 谢谢,现在它可以运行一次,但是一旦游戏更改了标题,脚本就不会再次运行。我怎样才能让它“监控”geht 标题并在每次游戏更改时更改它?
  • 你在这里混合了一些东西......上面是“清理”浏览器窗口的标题,只留下数字和冒号。现在,如果您想在某个时候恢复原始标题,则需要将其保存在 .replace() 之前,以便您可以将其替换回原始值。如果您想在游戏进行时保持数字更新,则需要实现一种方法,以便游戏定期在您的页面中调用 JS 函数,例如将新的计数器值作为参数传递给该函数,以便它可以在每次调用时替换数字。
【解决方案3】:

您的计时器/计数器似乎具有明确的格式。假设格式是##:##,你可以这样形成你的正则表达式:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2020-02-25
    • 1970-01-01
    相关资源
    最近更新 更多