|
// ==UserScript== |
|
// @name Backlog Banner |
|
// @namespace http://bouzuya.net/ |
|
// @version 0.1 |
|
// @description backlog banner |
|
// @author bouzuya |
|
// @match https://*.backlog.jp/find/* |
|
// @grant none |
|
// ==/UserScript== |
|
|
|
var API_KEY = ''; |
|
var bannerName = 'Banner'; |
|
|
|
var Backlog = function(options) { |
|
this.apiKey = options.apiKey; |
|
if (options.url) { |
|
this.spaceId = this._parseSpaceId(options.url); |
|
this.projectKey = this._parseProjectKey(options.url); |
|
} |
|
if (!this.apiKey) throw new Error('backlog banner: no api key'); |
|
if (!this.spaceId) throw new Error('backlog banner: no space id'); |
|
if (!this.projectKey) throw new Error('backlog banner: no project key'); |
|
}; |
|
|
|
Backlog.prototype.getWiki = function(wikiId, callback) { |
|
var wikiPath = this._getWikiPath({ |
|
apiKey: this.apiKey, |
|
wikiId: wikiId |
|
}); |
|
$.getJSON(wikiPath, {}, function(data, status, xhr) { |
|
callback(null, data); |
|
}); |
|
}; |
|
|
|
Backlog.prototype.getWikis = function(callback) { |
|
var wikisPath = this._getWikisPath({ |
|
apiKey: this.apiKey, |
|
projectKey: this.projectKey |
|
}); |
|
$.getJSON(wikisPath, {}, function(data, status, xhr) { |
|
callback(null, data); |
|
}); |
|
}; |
|
|
|
Backlog.prototype._parseProjectKey = function(url) { |
|
var pattern = '^https://[^\\.]+\\.backlog\\.jp/find/([^\\?]+)\\?'; |
|
var matched = url.match(pattern); |
|
return matched ? matched[1] : null; |
|
}; |
|
|
|
Backlog.prototype._parseSpaceId = function(url) { |
|
var pattern = '^https://([^\\.]+)\\.backlog\\.jp'; |
|
var matched = url.match(pattern); |
|
return matched ? matched[1] : null; |
|
}; |
|
|
|
Backlog.prototype._getWikiPath = function(options) { |
|
var wikiId = options.wikiId; |
|
var apiKey = options.apiKey; |
|
return '/api/v2/wikis/' + wikiId + '?apiKey=' + apiKey; |
|
}; |
|
|
|
Backlog.prototype._getWikisPath = function(options) { |
|
var projectKey = options.projectKey; |
|
var apiKey = options.apiKey; |
|
return '/api/v2/wikis?projectIdOrKey=' + projectKey + '&apiKey=' + apiKey; |
|
}; |
|
|
|
var showMessage = function(html) { |
|
var container = document.createElement('div'); |
|
container.innerHTML = html; |
|
var title = document.getElementById('mainTitle'); |
|
title.parentNode.insertBefore(container, title); |
|
}; |
|
|
|
var replaceNewlineToBr = function(s) { |
|
return s.replace(/[\r\n]+/g, '<br />'); |
|
}; |
|
|
|
var backlog = new Backlog({ url: location.href, apiKey: API_KEY }); |
|
backlog.getWikis(function(err, wikis) { |
|
var filtered = wikis.filter(function(i) { return i.name === bannerName; }); |
|
if (filtered.length === 0) { |
|
console.log('backlog banner: no banner page'); |
|
return; |
|
} |
|
var wikiId = filtered[0].id; |
|
backlog.getWiki(wikiId, function(err, wiki) { |
|
var html = replaceNewlineToBr(wiki.content); |
|
showMessage(html); |
|
}); |
|
}); |