通过用户脚本删除部分网站标题 [英] Remove part of title of website via userscript
问题描述
我尝试编写一个 userscript 代码,从网站(flash 浏览器游戏)的标题中删除所有内容,但倒计时开始操作时出现.
I try to code a userscript that removes everything from the title of a website (flash browsergame) but the countdown that appears when starting an action.
我是 JavaScript 新手,需要一些帮助.
I'm new to Javascript and need some help.
更新
正则表达式问题已解决,但我仍需要一些帮助才能让此脚本监视"标题,因此每次游戏更改时,脚本都会再次运行.
主标题如下所示:
摇晃&Fidget - 游戏(buffed buffed)
Shakes & Fidget - The Game (buffed buffed)
一旦开始一个动作,就会在开始处添加倒计时,因此标题更改为
As soon as an action is started a countdown is added to the beginning so the title changes to
02:26 - 摇晃 &Fidget - The Game (buffed buffed)
02:26 - Shakes & Fidget - The Game (buffed buffed)
我希望标题只显示倒计时.
I want the title to show the countdown only.
我在网上搜索并找到了不同的方法来做到这一点,但没有一个对我有用.
I searched the net and found different ways to do this but none of them works for me.
这是我目前拥有的:
// ==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 控制台显示有关正则表达式的错误.我试图转义字符,但错误是相同的:
The Javascript console shows errors concerning the regex. I tried to escape the characters but the errors are the same:
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)"发生了变化(它显示了服务器名称).
It has to be a regex match because the containing string "(buffed buffed)" changes (it shows the server name).
另一个问题是脚本应该监视"标题,因为它在每次开始或完成新操作时都会发生变化,但我的脚本只运行一次(在没有正则表达式的情况下进行了测试).
Another problem is that the script should "monitor" the title because it changes everytime a new action is started or finished but my script only runs once (tested it without regex).
预先感谢您的帮助,
懒人
推荐答案
对于正则表达式,使用:
For the regex, use:
document.title = document.title.replace (/[^0-9:]/g, "");
要检测标题更改,请使用 MutationObservers,这是一项新的 HTML5 功能在 Google Chrome 和 Firefox 中实现(两个主要的 userscripts 浏览器).
To detect title changes, use MutationObservers, a new HTML5 feature that is implemented in both Google Chrome and Firefox (The two main userscripts browsers).
这个完整的脚本会起作用:
// ==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()
.
这篇关于通过用户脚本删除部分网站标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!