-
Notifications
You must be signed in to change notification settings - Fork 3
/
custom-titlebar-text.js
123 lines (89 loc) · 3.49 KB
/
custom-titlebar-text.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Custom Titlebar Text
*
* Mozilla Firefox add-on lets you customize the
* titlebar text according to a rule. This changes
* the active window title and is useful for
* auto-type programs such as KeePass for entering
* correct account credentials.
*
* Source code: https://github.com/hrvojesolc/Custom-Titlebar-Text
* Add-on listing: https://addons.mozilla.org/firefox/addon/custom-titlebar-text
*
*/
// Set debug variable to true to enable console logging
var debug = false;
// Debug logging
if (debug) {
console.log('Debugging enabled.');
}
// Error handling function
function onError(error) {
console.log(`Error: ${error}`);
}
// Get current document title after page loads
var currentDocumentTitle = document.title;
// Debug logging
if (debug) {
console.log('Got current document title: ' + currentDocumentTitle);
}
// Default variables
var SelectedFormat = 'Default';
var CustomFormat = '{PageTitle}';
// Load current extension preference
var GetSelectedFormat = browser.storage.local.get('SelectedFormat');
GetSelectedFormat.then(loadSelectedFormat, onError);
// Execute based on extension preferences
function loadSelectedFormat(result) {
// Load current extension preference into a variable
var loadedSelectedFormat = result.SelectedFormat || 'Default';
// Debug logging
if (debug) {
console.log('Tried to restore "loadedSelectedFormat" value: ' + loadedSelectedFormat);
}
// If preference was Custom, perform variable substitutions based on custom format specified
if (loadedSelectedFormat == 'Custom') {
// Load current extension preference into a variable
var GetCustomFormat = browser.storage.local.get('CustomFormat');
GetCustomFormat.then(loadCustomFormat, onError);
// Execute based on custom format preference
function loadCustomFormat(result) {
// Variable for new document title
var newDocumentTitle = '';
// Load current extension preference into a variable
var loadedCustomFormat = result.CustomFormat || '{PageTitle}';
// Debug logging
if (debug) {
console.log('Tried to restore "loadedCustomFormat" value: ' + loadedCustomFormat);
}
// Perform substitutions for '{PageTitle}' in initial loadedCustomFormat variable
newDocumentTitle = loadedCustomFormat.replace('{PageTitle}', currentDocumentTitle);
// Perform substitution for '{PageUrl}' with full URL of page
newDocumentTitle = newDocumentTitle.replace('{PageUrl}', window.location.href);
// Perform substitution for '{PageDomain}'
newDocumentTitle = newDocumentTitle.replace('{PageDomain}', convertUrlToDomain(window.location.href));
// Perform substitution for '{PageHostname}'
newDocumentTitle = newDocumentTitle.replace('{PageHostname}', window.location.hostname);
// Set new document title
document.title = newDocumentTitle;
}
// If preference was not Custom, process defaults
} else {
// Leave default document title (do nothing).
// This isn't necessary and could be removed in future.
document.title = currentDocumentTitle;
}
}
// Function to convert URL to a subdomain (this is oversimplified and may not work on international TLDs)
function convertUrlToDomain(url, subdomain) {
subdomain = subdomain || false;
url = url.replace(/(https?:\/\/)?(www.)?/i, '');
if (!subdomain) {
url = url.split('.');
url = url.slice(url.length - 2).join('.');
}
if (url.indexOf('/') !== -1) {
return url.split('/')[0];
}
return url;
}