Last active
July 10, 2017 01:45
-
-
Save tkrkt/4c7f373faa66bf288c90d09e5f2f4965 to your computer and use it in GitHub Desktop.
Qiita filter by updated_at
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Qiita filter by updated_at | |
// @description Add "filter by updated_at" links to Qiita's search page | |
// @namespace https://gist.github.com/tkrkt | |
// @include http://qiita.com/search?* | |
// @version 5 | |
// @require https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js | |
// @require https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/locale/ja.js | |
// @require https://cdnjs.cloudflare.com/ajax/libs/URI.js/1.18.1/URI.min.js | |
// @grant none | |
// ==/UserScript== | |
const filters = [ | |
[7, 'd'], | |
[1, 'M'], | |
[3, 'M'], | |
[1, 'y'], | |
[3, 'y'] | |
]; | |
const search = URI(location.href).search(true); // e.g. search.q = 'tag:JavaScript updated:>2016-05-30' | |
const [dateParam, date] = search.q.match(/\s?updated:>([\d-]*)/) || []; // e.g. ['+updated:>2016-05-30', '2016-05-30'] | |
const currentFilter = date ? moment(date, 'YYYY-MM-DD').from(moment().startOf('day')) : '期間指定なし'; // e.g. '一ヶ月前' | |
const query = search.q.replace(dateParam, ''); // e.g. 'tag:JavaScript' | |
const span = document.createElement('span'); | |
span.style.padding = '6px 12px'; | |
span.style.display = 'block'; | |
const links = filters.map(([time, amount]) => { | |
const m = moment().startOf('day').subtract(time, amount); | |
return [' updated:>' + m.format('YYYY-MM-DD'), m.from(moment().startOf('day'))]; // e.g. ['+updated:>2016-05-30', '一ヶ月前'] | |
}); | |
const html = [['', '期間指定なし'], ...links].map(([filterParam, text]) => { | |
if (currentFilter === text) { | |
return `<span>${text.replace('前', '以内')}</span>`; | |
} else { | |
return `<a href="${URI(location.href).removeQuery('q').addQuery({q: query + filterParam})}">${text.replace('前', '以内')}</a>`; | |
} | |
}).join(' / '); | |
span.innerHTML = html; | |
const container = document.querySelector('form.searchResultContainer_form'); | |
container.appendChild(span); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment