How can I modify the title of a site using JavaScript?

南楼画角 提交于 2019-12-20 07:36:55

问题


When viewing a torrent page, the title of the tab is:

Bitsoup.org The Best site for your Torrent appetite :: Details for torrent "ABC"

where "ABC" is the name of the torrent file.

Unfortunately, when I have 3+ tabs open, I can not see what the name of the torrent file is.

I am attempting to create a Greasemonkey script to truncate, or split, the title, so that it only displays the torrent-name, and not the beginning part.

I am aware that you can modify the title associated with a page by using document.title = ...

but I am unsure of what to assign it to.


回答1:


You could use a regular expression, perhaps:

document.title = document.title.match(/"([^"]+)"/)[1]

This regex uses grouping to matches the first thing that is between quotes, and assigns it to the title (without the quotes).




回答2:


Using a substr method is by far the simplest if you know that the beginning of the title will always be identical. For example, with the title you gave:

document.title = document.title.substr(76);

If it is not a static title, then RegEx would be the next most logical route. To match the last set of quotes and ignore potential whitespace:

document.title = document.title.match(/"([^"]+)"\s*$/)[1];


来源:https://stackoverflow.com/questions/11253058/how-can-i-modify-the-title-of-a-site-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!