<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>~iany/ Browser</title><link>https://blog.iany.me/tags/browser/</link><description>Recent content in Browser «~iany/»</description><language>en-US</language><managingEditor>me@iany.me (Ian Yang)</managingEditor><webMaster>me@iany.me (Ian Yang)</webMaster><copyright>CC-BY-SA 4.0</copyright><lastBuildDate>Fri, 21 Jul 2023 20:31:59 +0800</lastBuildDate><atom:link href="https://blog.iany.me/tags/browser/index.xml" rel="self" type="application/rss+xml"/><item><title>Renaming Browser Tab Names</title><link>https://blog.iany.me/2023/07/renaming-browser-tab-names/</link><pubDate>Fri, 21 Jul 2023 20:31:59 +0800</pubDate><author>me@iany.me (Ian Yang)</author><guid>https://blog.iany.me/2023/07/renaming-browser-tab-names/</guid><description>&lt;p&gt;Renaming browser tab names may seem like a simple task, but it can actually be quite challenging.&lt;/p&gt;
&lt;p&gt;Most browsers sync the tab name with the web page title. Therefore, it seems simple to set tab name by setting &lt;code&gt;document.title&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-javascript"&gt;document.title = &amp;quot;Custom Tab Name&amp;quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However, many web pages use JavaScript to alter the page title, which would override the custom name. While &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty"&gt;&lt;code&gt;Object.defineProperty&lt;/code&gt;&lt;/a&gt; in JavaScript can override the property setter function, it&amp;rsquo;s not possible to access the original setter function for &lt;code&gt;document.title&lt;/code&gt;. Fortunately, browsers sync page titles with the first &lt;code&gt;&amp;lt;title&amp;gt;&lt;/code&gt; tag, so setting its content acts as a workaround to set the page title.&lt;/p&gt;
&lt;p&gt;Here is the bookmark for renaming a tab. I also add &lt;code&gt;%t&lt;/code&gt; as the token for the original page tab title. This is handy such as adding a custom prefix with &lt;code&gt;Work: %t&lt;/code&gt;, or restoring the original title without reloading the page by using &lt;code&gt;%t&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-javascript"&gt;// Rename the document title using a bookmarklet.
//
// As a user,
// - Once I have renamed the tab, it should not be overwritten.
// - I can include the token %t as placeholder for the real title.
// - To restore, I can rename the tab to %t
//
// Install: Copy the code to
//
// https://caiorss.github.io/bookmarklet-maker/
//
// and generate the bookmarklet.
// Init only once
if (!(&amp;quot;_renameTitle&amp;quot; in document)) {
// Force creating the title tag.
document.title = document.title || &amp;quot;&amp;quot;;
const titleEl = document.getElementsByTagName(&amp;quot;title&amp;quot;)[0];
const titleTokenRegex = /%t/g;
// Remembers the real title
let titleWithoutRenaming = document.title;
// User set title
let titleWithRenaming = &amp;quot;&amp;quot;;
// Rename the document title to v.
// If v contains the token %t, replace all occurences to the
// real title.
document._renameTitle = (v) =&amp;gt; {
titleWithRenaming = v;
titleEl.innerText = v.replace(titleTokenRegex, titleWithoutRenaming);
};
Object.defineProperty(document, &amp;quot;title&amp;quot;, {
// Other code will use document.title setter to change the title.
//
// Remember the new value as the real title but still use the
// title set by user.
set: (v) =&amp;gt; {
titleWithoutRenaming = v;
// Once document has defined the title property, its value is
// not synchronized to the tab name.
//
// Here uses a workaround to set the title tag content.
titleEl.innerText = titleWithRenaming.replace(titleTokenRegex, v);
},
get: () =&amp;gt; titleEl.innerText,
});
}
const title = prompt(
&amp;quot;Rename tab (Use token %t for original title)&amp;quot;,
document.title
);
// title is null when user cancel the dialog.
if (title !== null) {
document._renameTitle(title);
}
&lt;/code&gt;&lt;/pre&gt;</description><category domain="https://blog.iany.me/post/">Posts</category><category domain="https://blog.iany.me/tags/browser/">Browser</category><category domain="https://blog.iany.me/tags/javascript/">JavaScript</category><category domain="https://blog.iany.me/tags/productivity/">Productivity</category></item></channel></rss>