// ==UserScript==
// @name Feedly Search
// @namespace http://nodaguti.usamimi.info/
// @description Add search box on Feedly
// @include http://feedly.com/*
// @include https://feedly.com/*
// @version 0.1
// @author nodaguti
// @license MIT License
// @grant GM_log
// @grant GM_addStyle
// @grant unsafeWindow
// ==/UserScript==
(function(window, document){
var DB_NAME = 'feedly-search-entries';
var DB_VERSION = 1;
var DB_STORE_NAME = 'entries';
var DB = null;
var timeline = document.getElementById('box');
var SEARCH_ICON = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABGdBTUEAALGPC/xhBQAACkFpQ0NQSUNDIFByb2ZpbGUAAEgNnZZ3VFPZFofPvTe90BIiICX0GnoJINI7SBUEUYlJgFAChoQmdkQFRhQRKVZkVMABR4ciY0UUC4OCYtcJ8hBQxsFRREXl3YxrCe+tNfPemv3HWd/Z57fX2Wfvfde6AFD8ggTCdFgBgDShWBTu68FcEhPLxPcCGBABDlgBwOFmZgRH+EQC1Py9PZmZqEjGs/buLoBku9ssv1Amc9b/f5EiN0MkBgAKRdU2PH4mF+UClFOzxRky/wTK9JUpMoYxMhahCaKsIuPEr2z2p+Yru8mYlybkoRpZzhm8NJ6Mu1DemiXho4wEoVyYJeBno3wHZb1USZoA5fco09P4nEwAMBSZX8znJqFsiTJFFBnuifICAAiUxDm8cg6L+TlongB4pmfkigSJSWKmEdeYaeXoyGb68bNT+WIxK5TDTeGIeEzP9LQMjjAXgK9vlkUBJVltmWiR7a0c7e1Z1uZo+b/Z3x5+U/09yHr7VfEm7M+eQYyeWd9s7KwvvRYA9iRamx2zvpVVALRtBkDl4axP7yAA8gUAtN6c8x6GbF6SxOIMJwuL7OxscwGfay4r6Df7n4Jvyr+GOfeZy+77VjumFz+BI0kVM2VF5aanpktEzMwMDpfPZP33EP/jwDlpzcnDLJyfwBfxhehVUeiUCYSJaLuFPIFYkC5kCoR/1eF/GDYnBxl+nWsUaHVfAH2FOVC4SQfIbz0AQyMDJG4/egJ961sQMQrI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\
\
.invisible{\
visibility: hidden !important;\
}\
\
#feedlySearchBoxContainer{\
position: absolute;\
top: 0;\
right: 0;\
z-index: 99999;\
color: rgb(102, 102, 102);\
background-color: rgb(245, 245, 245);\
box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);\
border: 1px solid rgb(190, 190, 190);\
padding: 1em;\
}\
\
#feedlySearchBoxContainer input[type='text']{\
border: 1px #bfbfbf solid;\
border-radius: 3px;\
color: #444;\
padding: 3px;\
}\
#feedlySearchBoxContainer button,\
#feedlySearchBoxContainer input[type='checkbox'],\
#feedlySearchBoxContainer select{\
background-image: linear-gradient(to bottom, #ededed, #ededed 38%, #dedede);\
border: 1px #ccc solid;\
border-radius: 3px;\
box-shadow: 0 1px 0 rgba(0, 0, 0, 0.1);\
color: #444;\
text-shadow: 0 1px 0 #f0f0f0;\
padding: 2px 10px;\
margin: 0 5px;\
}\
#feedlySearchBoxContainer button::-moz-focus-inner{\
border: 0 !important;\
padding: 0 !important;\
}\
#feedlySearchBoxContainer form{\
margin: 0;\
}\
\
#feedlySearchOptions{\
margin-top: 5px;\
font-size: 90%;\
color: #333;\
}\
\
#feedlySearchLoading{\
width: 12px;\
height: 12px;\
border-radius: 50%;\
border: 3px solid #333;\
border-right-color: transparent;\
animation: spin 1s linear infinite;\
display: inline-block;\
vertical-align: middle;\
margin-left: 0.5em;\
}\
\
@keyframes spin{\
0% { transform: rotate(0deg); opacity: 0.2; }\
50% { transform: rotate(180deg); opacity: 1.0; }\
100% { transform: rotate(360deg); opacity: 0.2; }\
}\
\
#feedlySearchHitList{\
list-style: none;\
margin-top: 3em;\
}\
#feedlySearchHitList > li{\
margin: 1em 0;\
list-style: none;\
}\
#feedlySearchHitList a{\
color: #1122CC !important;\
}\
.feedlySearchResultTitle{\
font-size: 130%;\
}\
.feedlySearchResultSource{\
display: inline-block;\
margin-left: 0.5em;\
font-size: 80%;\
color: #888;\
}\
.feedlySearchResultSource::before{\
content: '(';\
}\
.feedlySearchResultSource::after{\
content: ')';\
}\
.feedlySearchResultBody{\
padding: 1em 0 0 2.5em;\
width: 80%;\
font-size: 110%;\
max-height: 200px;\
overflow: hidden;\
}\
.feedlySearchResultBody:hover{\
max-height: auto;\
}";
var FeedlySearch = {
init: function(){
//Add observer
var observer = new MutationObserver(function(mutations){
this.addEntries();
}.bind(this));
observer.observe(timeline, { childList: true, subtree: true });
//Open database
this.openDatabase();
//Add search button after waiting for building the page
setTimeout(this.addSearchButton, 3000);
GM_addStyle(STYLE);
},
addSearchButton: function(){
//Search Button
var img = document.createElement("img");
img.id = "pageActionSearch";
img.className = "pageAction";
img.width = "20";
img.height = "20";
img.border = "0";
img.src = SEARCH_ICON;
img.dataset.appAction = "search";
img.title = "Search";
var parent = document.querySelector("#feedlyPageHeader > .pageActionBar");
if(!parent) return;
parent.appendChild(img);
img.addEventListener('click', function(){
var rect = document.getElementById('feedlyPageHeader').getBoundingClientRect();
var searchbox = document.getElementById('feedlySearchBoxContainer');
//Adjust position of search box
searchbox.style.top = (rect.bottom + 5) + 'px';
searchbox.style.right = (document.documentElement.clientWidth - rect.right) + 'px';
//Show search box
searchbox.classList.toggle('hidden');
//Focus search box
document.getElementById('feedlySearchBox').focus();
}, false);
//Search Box
var searchBoxTag = '' +
'<form action="" onsubmit="FeedlySearch.search(document.getElementById(\'feedlySearchBox\').value);return false;">'+
'<input type="text" id="feedlySearchBox" title="Split by whitepace to AND search" />'+
'<button type="submit">Search</button>'+
'<div id="feedlySearchLoading" class="invisible" title="Click to abort searching"></div>'+
'<div id="feedlySearchOptions">'+
'<label><input type="checkbox" id="feedlySearchTitle" checked="checked" /> Title</label> '+
'<label><input type="checkbox" id="feedlySearchURL" checked="checked" /> URL</label> '+
'<label><input type="checkbox" id="feedlySearchBody" checked="checked" /> Body</label> '+
'<label><input type="checkbox" id="feedlySearchRegExp" /> RegExp</label>'+
'</div>'+
'</form>';
var container = document.createElement('div');
container.id = 'feedlySearchBoxContainer';
container.classList.add('hidden');
document.body.appendChild(container);
container.innerHTML = searchBoxTag;
setTimeout(function(){
document.getElementById('feedlySearchLoading').addEventListener('click', function(){
FeedlySearch.abortSearch();
}, false);
});
},
openDatabase: function(){
var req = window.indexedDB.open(DB_NAME, DB_VERSION);
req.onerror = this.onError;
req.onupgradeneeded = this.createDatabase;
req.onsuccess = function(event){
GM_log('Success: Opening the database.');
DB = event.target.result;
};
},
createDatabase: function(event){
var objectStore = event.target.result.createObjectStore(DB_STORE_NAME, { keyPath: "id" });
GM_log("Success: Creating objectStore.");
},
resetDatabase: function(){
DB.transaction([DB_STORE_NAME], "readwrite").objectStore(DB_STORE_NAME).clear();
},
addEntries: function(){
GM_log("Adding new entries...");
var transaction = DB.transaction([DB_STORE_NAME], "readwrite");
transaction.onerror = this.onError;
transaction.oncomplete = this.onAllEntriesAdded;
var objectStore = transaction.objectStore(DB_STORE_NAME);
//unread articles
var unreadEntries = Array.slice(timeline.getElementsByClassName('u0Entry')).filter(function(item){
return item.getElementsByClassName('unread').length > 0;
});
unreadEntries.forEach(function(entry){
var id = entry.dataset.inlineentryid;
var title = entry.dataset.title;
var url = entry.dataset.alternateLink;
var sourceTitle = entry.querySelector('.sourceTitle > a');
var summary = entry.getElementsByClassName('u0Summary')[0].innerHTML;
var request = objectStore.put({
id: id,
title: title,
url: url,
sourceTitle: sourceTitle.firstChild.nodeValue,
sourceURL: sourceTitle.href,
body: summary,
});
request.onsuccess = FeedlySearch.onEntryAdded;
request.onerror = FeedlySearch.onError;
});
//opened articles
var selectedEntry = timeline.querySelector('.inlineFrame[data-uninlineentryid] .u100Entry');
if(selectedEntry){
var id = selectedEntry.dataset.selectentryid;
var title = selectedEntry.dataset.title;
var url = selectedEntry.dataset.alternateLink;
var sourceTitle = selectedEntry.getElementsByClassName('sourceTitle')[0];
var body = selectedEntry.getElementsByClassName('entryBody')[0];
var fullFeedLoaded = body.classList.contains('gm_fullfeed_loaded');
var content = fullFeedLoaded ? body : body.querySelector('.content');
var request = objectStore.put({
id: id,
title: title,
url: url,
sourceTitle: sourceTitle.firstChild.nodeValue,
sourceURL: sourceTitle.href,
body: content.innerHTML,
});
request.onsuccess = this.onEntryAdded;
request.onerror = this.onError;
}
//If remove the following code, this script doesn't work well. (I don't know why)
if(objectStore.mozGetAll)
objectStore.mozGetAll().onsuceess = function(event){};
},
onEntryAdded: function(event){
GM_log("Entry Saved: " + event.target.result);
},
onAllEntriesAdded: function(event){
GM_log("Finish Saving All Entries.");
},
search: function(key){
GM_log("Searching...");
this._abortSearch = false;
var count = 0;
var objectStore = DB.transaction([DB_STORE_NAME]).objectStore(DB_STORE_NAME);
//Get search options
var optionTags = Array.slice(document.getElementById('feedlySearchOptions').querySelectorAll('input[type="checkbox"]'));
var options = {};
var keys;
optionTags.forEach(function(optionTag){
options[optionTag.id.replace('feedlySearch', '').toLowerCase()] = optionTag.checked;
});
//Create RegExp Object if RegExp option selected
if(options.regexp){
keys = [new RegExp(key)];
}else{
keys = key.split(/[\s ]+/);
}
//Create Search Display
var titleBar = document.getElementById('feedlyTitleBar');
var hhint = titleBar.getElementsByClassName('hhint')[0];
//Change Title to "Search"
titleBar.firstChild.nodeValue = 'Search';
hhint.innerHTML = '';
//Show Loading icon
var loadingIcon = document.getElementById('feedlySearchLoading');
loadingIcon.classList.remove('invisible');
//Clear timeline
var entriesArea = document.getElementById('mainArea');
while(entriesArea.hasChildNodes()){
entriesArea.removeChild(entriesArea.firstChild);
}
//Create List
var hitEntriesList = document.createElement('ul');
hitEntriesList.id = "feedlySearchHitList";
entriesArea.appendChild(hitEntriesList);
var startTime = Date.now();
//Emphasize every hit term
function emphasizeTerm(str, keys){
var _str = str;
keys.forEach(function(key){
_str = _str.replace(key, "<strong>$&</strong>", "g");
});
return _str;
}
//Search
objectStore.openCursor().onsuccess = function(event){
var cursor = event.target.result;
if(cursor){
var entry = cursor.value;
if(
keys.every(function(key){
return options.regexp ?
(options.title && key.test(entry.title)) ||
(options.url && key.test(entry.url)) ||
(options.body && key.test(entry.body))
:
(options.title && entry.title.indexOf(key) > -1) ||
(options.url && entry.url.indexOf(key) > -1) ||
(options.body && entry.body.indexOf(key) > -1)
})
){
count++;
hitEntriesList.insertAdjacentHTML("beforeend", "" +
"<li>"+
'<div class="feedlySearchResultTitle">' +
'<a href="' + entry.url + '" target="_blank">' + emphasizeTerm(entry.title, keys) + "</a>" +
'<div class="feedlySearchResultSource">' +
'<a href="' + entry.sourceURL + '">' + entry.sourceTitle + "</a>" +
"</div>" +
"</div>" +
'<div class="feedlySearchResultBody">' +
emphasizeTerm(entry.body, keys) +
'</div>' +
"</li>");
}
if(!FeedlySearch._abortSearch) return cursor.continue();
}
loadingIcon.classList.add('invisible');
GM_log("Search finished.");
if(count == 0){
entriesArea.innerHTML = "No Entries Found.";
}else{
hhint.innerHTML = count + ' results (' + ((Date.now() - startTime) / 1000) + ' seconds)';
}
}
},
abortSearch: function(){
this._abortSearch = true;
},
onError: function(event){
GM_log('Error has occurred.\n\nType: ' + event.type + '\nValue: ' + event.value);
}
};
window.FeedlySearch = FeedlySearch;
FeedlySearch.init();
})(unsafeWindow, unsafeWindow.document);