FDZ Keyword Hightlight
FDZ Title Keyword Hightlight,加入下列兩項功能
// ==UserScript==
// @name FDZ Keyword Hightlight
// @description 自動高亮文章標題關鍵字
// @author NKid
// @version 2011-04-24
// @run-at document-end
// @include http://forum.fdzone.org/forumdisplay.php?fid=*
// @include http://forum.*.fdzone.org/forumdisplay.php?fid=*
// ==/UserScript==
GM_registerMenuCommand('FDZ Keyword Hightlight - Setting Keywords', settingKeywords);
GM_registerMenuCommand('FDZ Keyword Hightlight - Keywords List', listKeywords);
GM_registerMenuCommand('FDZ Keyword Hightlight - Setting Authors', settingAuthors);
GM_registerMenuCommand('FDZ Keyword Hightlight - Authors List', listAuthors);
GM_registerMenuCommand('FDZ Keyword Hightlight - Open Link in New Tab', openNewTab);
//GM_registerMenuCommand('FDZ Title Find RegExp - DEL', DEL);
GM_addStyle('.GM_gotit {color:#000;background-color:#FBED73;-moz-border-radius:3px;padding:0 2px;}'); //Highlight Keyword Style
GM_addStyle('#GM_newtab {-moz-border-radius: 3px; color: #000000; cursor: pointer; font-size: 10px; height: 10px; position: fixed; right: 3px; top: 3px; width: 10px;} .GM_newtabOn {background-color: #00FF00} .GM_newtabOff {background-color: #FF0000}'); //NewTab State
/*function DEL() {GM_deleteValue("keywords");}*/
var tar=GM_getValue('newtab'); //new tab state
//setting Keywords
function settingKeywords() {
var KWs = (decodeURI(GM_getValue("keywords")) == 'undefined') ? '' : decodeURI(GM_getValue("keywords"));
var newKWs = prompt('Please Enter the Keywords.\n(use commas to separate)', KWs).split(',');
newKWs.sort(descStrLen);
GM_setValue("keywords", encodeURI(newKWs.join()));
refreshPage();
}
//list Keywords
function listKeywords() {
var listKWs = decodeURI(GM_getValue("keywords")).split(',');
alert(listKWs.join('\n'));
}
//setting Authors
function settingAuthors() {
var ARs = (decodeURI(GM_getValue("authors")) == 'undefined') ? '' : decodeURI(GM_getValue("authors"));
var newARs = prompt('Please Enter Authors\' names.\n(use commas to separate)', ARs).split(',');
newARs.sort(descStrLen);
GM_setValue("authors", encodeURI(newARs.join()));
refreshPage();
}
//list Authors
function listAuthors() {
var listARs = decodeURI(GM_getValue("authors")).split(',');
alert(listARs.join('\n'));
}
//Open articles in new tab
function openNewTab() {
GM_setValue("newtab", confirm("Open articles in new tab?"));
refreshPage();
}
//sort function (by string length)
function descStrLen(a, b) {
if (a.length < b.length) return 1;
}
//Message Refresh Page
function refreshPage() {
alert('Please Refresh Page.');
}
function toRegExpArray(oriArray) {
//把關鍵字array轉換成RegExp Array
var reArray = new Array(); //RegExp Array
for (var i = 0; i < oriArray.length; i++) {
reArray.push(new RegExp('(' + oriArray[i] + ')','ig'));
}
return reArray;
}
function main() {
//文章標題、描述
var KWs = decodeURI(GM_getValue('keywords')).split(','); //keywords array
var titles = document.querySelectorAll('span[id^=thread_]'); //all articles' title
var descs=document.querySelectorAll("[id^='desc_']"); //all articles' description
//作者
var ARs = decodeURI(GM_getValue('authors')).split(','); //authors array
var authors=document.querySelectorAll('.author > cite > a'); //all authors
//轉換RegExp Array
var reKWs=toRegExpArray(KWs);
var reARs=toRegExpArray(ARs);
//比對關鍵字
var i, j, titleStr, descStr,authorStr;
for (i = 0; i < titles.length; i++) {
//取出文章標題超連結的文字
titleStr = titles[i].childNodes[0].innerHTML;
//比對關鍵字
for (j = 0; j < reKWs.length; j++) {
if (reKWs[j].test(titleStr)) titleStr = titleStr.replace(reKWs[j], '<span class=\"GM_gotit\">$1</span>');
}
titles[i].childNodes[0].innerHTML = titleStr;
titles[i].childNodes[0].target=(tar)?"_blank":"";
titles[i].childNodes[0].href=titles[i].childNodes[0].href.replace(/&i.*/i,''); //FDZ Article Link Clean
}
for (i = 0; i< descs.length; i++) {
//取出文章描述的文字
descStr = descs[i].innerHTML;
//比對關鍵字
for (j = 0; j < reKWs.length; j++)
if (reKWs[j].test(descStr)) descStr = descStr.replace(reKWs[j], '<span class=\"GM_gotit\">$1</span>');
descs[i].innerHTML = descStr;
}
for (i = 0; i< authors.length; i++) {
//取出文章描述的文字
authorStr = authors[i].innerHTML;
//比對關鍵字
for (j = 0; j < reARs.length; j++) {
if (reARs[j].test(authorStr)) authorStr = authorStr.replace(reARs[j], '<span class=\"GM_gotit\">$1</span>');
}
authors[i].innerHTML = authorStr;
}
}
function newtab() {
var newtabState = document.createElement("div");
newtabState.id="GM_newtab";
newtabState.title="Open articles in newtab ?";
newtabState.className=(tar)?"GM_newtabOn":"GM_newtabOff";
document.getElementsByTagName("body")[0].appendChild(newtabState);
document.getElementById('GM_newtab').addEventListener('click',function(){GM_setValue("newtab", 1-GM_getValue("newtab")); refreshPage(); },false);
}
main();
newtab();