编程语言
首页 > 编程语言> > javascript-通过用户脚本删除网站标题的一部分

javascript-通过用户脚本删除网站标题的一部分

作者:互联网

我尝试编写一个userscript的代码,该代码从网站的标题(Flash浏览器游戏)中删除所有内容,但在开始操作时出现倒数计时.

我是Java语言的新手,需要帮助.

UPDATE

regexp问题已解决,但我仍然需要一些帮助来使此脚本“监视”标题,因此每次游戏更改它时,脚本都会再次运行.

主标题看起来像这样:

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)”会发生变化(它显示服务器名称).

另一个问题是该脚本应“监视”标题,因为每次启动或完成新操作时它都会更改,但是我的脚本仅运行一次(未经regex对其进行测试).

在此先感谢您的帮助,

slaecker

解决方法:

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

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

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

标签:userscripts,javascript,userscripts
来源: https://codeday.me/bug/20191014/1911876.html