Manage page titles per (sub)domain. Shift-T-M for options.
当前为
// ==UserScript==
// @name Title Manager
// @namespace grom
// @description Manage page titles per (sub)domain. Shift-T-M for options.
// @include http*
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_deleteValue
// @version 1.0.20140312
// @run-at document-end
// ==/UserScript==
if ( GM_getValue === 'undefined' || GM_setValue === 'undefined' ) {
alert('=== Title Manager ===\n\nUnable to load or store values.\
\nPlease use supported script managers:\nGreasemonkey, Tampermonkey...');
}
var domains = GM_getValue('domains', '').replace(/\./g, '\\.');
var match = window.location.host.toLowerCase().match(domains);
if (match) {
var _find = GM_getValue(match[0] + '-find', '');
if (_find.match(/^regex:/)) {
var _find = _find.replace(/^regex:/, '');
var _find = new RegExp(_find, 'i');
}
var _replaceWith = GM_getValue(match[0] + '-with', '');
}
var t = document.querySelector('title');
var tOriginal = t.innerHTML;
if (t && _find) {
t.innerHTML = t.innerHTML.replace(_find,_replaceWith);
}
// separators
var sep1 = '========';
var sep2 = '--------';
function tm_add() {
var d = document,
_domain = d.getElementById('tm-domain').value.toLowerCase(),
_domainCheck = /^[a-z0-9_\.-]+$/;
if (!_domain.match(_domainCheck)) {
d.getElementById('tm-error-notifier').innerHTML = 'Domain invalid. Please use letters a-z, numbers 0-9, underscore _, minus - or dot .';
return false;
}
var _find = d.getElementById('tm-find').value;
var _with = d.getElementById('tm-with').value;
// store values
if (_domain && _find) {
var domains = GM_getValue('domains', '');
if (!domains) {
GM_setValue('domains', _domain);
}
else {
var match = _domain.replace(/\./g, '\\.');
var match = new RegExp('(^|\\|)' + match + '($|\\|)');
if (!domains.match(match)) {
var domains = domains + '|' + _domain;
GM_setValue('domains', domains);
}
}
GM_setValue(_domain + '-find', _find);
if (_with) {
var _withRegex = d.getElementById('tm-with');
// if not adding as regex
if (!_withRegex.getAttribute('regex')) {
// crazy dollar sign escaping;
var _with = _with.replace(/\$/g, '$$$$');
}
GM_setValue(_domain + '-with', _with);
_withRegex.removeAttribute('regex');
}
}
}
function tm_addRegex() {
var _find = document.getElementById('tm-find').value;
if (!_find.match(/^regex:/)) {
_find = _find.replace(/^/, 'regex:');
document.getElementById('tm-find').value = _find;
}
document.getElementById('tm-with').setAttribute('regex', 'true');
tm_add();
}
function tm_manage() {
var d = document;
// d.documentElement.innerHTML is alternative to d.write("");
d.documentElement.innerHTML = '';
var domains = GM_getValue('domains', '');
if (domains) {
var domains = domains.split('|');
for(var i = 0, j = domains.length; i < j; i++) {
var _find = GM_getValue(domains[i] + '-find', '');
var _replaceWith = GM_getValue(domains[i] + '-with', '');
var box = d.createElement('div');
box.className = 'item';
box.innerHTML = '<span class="doms">' + domains[i] + '</span><br /><span class="find">' + _find + '</span>\
<br /><span class="with">' + _replaceWith + '</span><br /><button>Remove</button><br /><span>' + sep1 + '</span>';
if (d.body) d.body.appendChild(box);
else d.documentElement.appendChild(box);
box.getElementsByTagName('button')[0].addEventListener('click', tm_removeItem);
}
}
var impList = d.createElement('textarea');
impList.id = 'tm-import-list';
d.documentElement.appendChild(impList);
var imp = d.createElement('input');
imp.type = 'submit'; imp.id = 'tm-import'; imp.value = 'Import';
d.documentElement.appendChild(imp);
imp.addEventListener('click', tm_import);
}
function tm_removeItem() {
var item = this.parentNode;
var _domain = item.getElementsByClassName('doms')[0].innerHTML;
GM_deleteValue(_domain + '-find');
GM_deleteValue(_domain + '-with');
var domains = GM_getValue('domains', '');
var match = _domain.replace(/\./g,"\\.");
var match = new RegExp('(^|\\|)' + match + '($|\\|)'); // match: (^ or |) + single/current domain + ($ or |)
var domains = domains.replace(match, '$1').replace(/(\|)\||\|$/g, '$1'); // replace: matched single domain with ^ or |; replace: || with | or remove |$
GM_setValue('domains', domains);
item.parentNode.removeChild(item);
}
function tm_import() {
var d = document,
list = d.getElementById('tm-import-list').value;
var list = list.match(/.+/g).join(sep2).replace(/(--------)+========$/, '').split(sep1+sep2);
if (!d.getElementById('tm-add')) tm_QuickMenu();
for(var i = 0, j = list.length; i < j; i++) {
var listB = list[i].split(sep2);
d.getElementById('tm-domain').value = listB[0];
d.getElementById('tm-find').value = listB[1];
if (listB[2]) {
d.getElementById('tm-with').value = listB[2];
d.getElementById('tm-with').setAttribute('regex', 'true');
}
if (listB[0] && listB[1]) tm_add();
}
tm_manage();
}
function tm_QuickMenu() {
var d = document,
box = d.createElement('div');
box.style = 'text-align:center!important;';
box.innerHTML = '<br /><hr><h1>Title Manager</h1><p>Full title: "<strong>' + tOriginal + '</strong>"</p>\
<p id="tm-error-notifier">Regex supported only in "Search for"; use single backslash <strong>\\</strong> for escaping characters.</p>\
<p>Domain: <input type="text" id="tm-domain" value="' + window.location.host.toLowerCase() + '" /></p>\
<p>Search for: <input type="text" id="tm-find" value="' + tOriginal + '" /></p>\
<p>Replace with: <input type="text" id="tm-with" value="" /></p>\
<p><input type="submit" id="tm-add" value="Add" /> or <input type="submit" id="tm-add-regex" value="Add as regex" /></p>\
<p><input type="submit" id="tm-manage" value="View and manage all title rules" /></p><br /><br />';
if (d.body) d.body.appendChild(box);
else d.documentElement.appendChild(box);
box.scrollIntoView();
d.getElementById('tm-add').addEventListener('click', tm_add);
d.getElementById('tm-add-regex').addEventListener('click', tm_addRegex);
d.getElementById('tm-manage').addEventListener('click', tm_manage);
}
// trigger for QuickMenu: Shift+T+M
var kbd = [];
onkeydown = onkeyup = function(e) {
kbd[e.keyCode] = e.type == 'keydown';
if (kbd[16] && kbd[84] && kbd[77]) {
tm_QuickMenu();
kbd = [];
return false;
}
}