Last active
February 1, 2017 00:08
-
-
Save tkrkt/6fd3531a100cdf2f1f567ba700e9a94f to your computer and use it in GitHub Desktop.
Qiita filter by created_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 created_at | |
// @description Add "filter by created_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 created:>2016-05-30' | |
const [dateParam, date] = search.q.match(/\s?created:>([\d-]*)/) || []; // e.g. ['+created:>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 [' created:>' + m.format('YYYY-MM-DD'), m.from(moment().startOf('day'))]; // e.g. ['+created:>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