forked from ProLoser/Github-Omnibox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecorate.js
139 lines (122 loc) · 4.95 KB
/
decorate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
function Decorator() {
// <span class="author"><a href="/proloser" class="url fn" itemprop="url" rel="author"><span itemprop="title">proloser</span></a></span>
var author = document.querySelector('.author');
this.owner = author.textContent;
// <strong><a href="/proloser/github-omnibox" data-pjax="#js-repo-pjax-container">github-omnibox</a></strong>
this.repo = author.nextElementSibling.nextElementSibling.textContent;
}
Decorator.prototype.owner = '';
Decorator.prototype.repo = '';
Decorator.prototype.isPublic = function() {
var isPublic = document.querySelector('.entry-title');
return isPublic && isPublic.classList.contains('public');
};
Decorator.prototype.img = function(src, alt) {
return '<img src="'+src+'" alt="'+alt+'" />';
};
Decorator.prototype.row = function(data) {
var isIconUrl = data.icon && data.icon.substr(0, 4) == 'http';
var icon;
if (isIconUrl)
icon = '<span class="octicon">' + this.img(data.icon, data.name) + '</span>';
else
icon = '<span class="octicon octicon-' + data.icon +'"></span>';
var tmpl = '<li class="github-omnibox-sidebar-item tooltipped tooltipped-w" aria-label="'+data.name+'"> \
<a href="'+data.url+'" class="sunken-menu-item">'+icon+' \
<span class="full-word">'+ (data.badge && this.img(data.badge, data.name) || data.name) +'</span> \
</a> \
</li>';
return tmpl.replace(/:owner/g, this.owner).replace(/:repo/g, this.repo);
};
Decorator.prototype.addMenu = function(items) {
var target = document.querySelector('.sunken-menu');
if (!target) return;
var element = document.createElement('div');
element.className = 'sunken-menu-separator';
target.appendChild(element);
element = document.createElement('ul');
element.className = 'sunken-menu-group omnibox-menu';
element.innerHTML = items.map(this.row, this).join('');
[].forEach.call(element.querySelectorAll('img'), function(el){
el.addEventListener('error', function(event){
// event.srcElement.outerHTML = event.srcElement.alt;
// <img> <span> <a> <li>
event.srcElement.parentElement.parentElement.parentElement.outerHTML = '';
});
});
target.appendChild(element);
};
Decorator.prototype.addUrl = function() {
// Is there a gh-pages branch but no project URL?
if (!document.querySelector('.repository-website') && document.querySelector('[data-name="gh-pages"]')) {
var target = document.querySelector('.repository-description');
if (target) {
element = document.createElement('div');
var url = this.owner + '.github.io';
if (!this.repo.endsWith('.github.io') && !this.repo.endsWith('.github.com'))
url += '/' + this.repo;
element.className = 'repository-website js-details-show';
element.innerHTML = '<p><a href="http://'+url+'" class="edit-link">'+url+'</a></p>';
target.parentNode.insertBefore(element, target.nextSibling);
}
}
};
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
var menu = [
{
name: 'Gitter Chat',
icon: 'https://cdn01.gitter.im/_s/1b5c955/images/favicon5.png',
badge: 'https://badges.gitter.im/:owner/:repo.png',
url: 'https://gitter.im/:owner/:repo'
},
{
name: 'Travis CI',
icon: 'https://travis-ci-org.global.ssl.fastly.net/images/favicon-04004642a94f6c2b0d4a567fb5dbe145.png',
badge: 'https://api.travis-ci.org/:owner/:repo.svg',
url: 'https://travis-ci.org/:owner/:repo'
},
{
name: 'Gemnasium',
icon: 'https://assets.gemnasium.com/assets/favicon.png',
badge: 'https://david-dm.org/:owner/:repo.svg',
url: 'https://david-dm.org/:owner/:repo'
},
{
name: 'David DM',
icon: 'https://david-dm.org/favicon.ico',
badge: 'https://david-dm.org/:owner/:repo.svg',
url: 'https://david-dm.org/:owner/:repo'
},
{
name: 'David DM Dev',
icon: 'https://david-dm.org/favicon.ico',
badge: 'https://david-dm.org/:owner/:repo/dev-status.svg',
url: 'https://david-dm.org/:owner/:repo#info=devDependencies'
},
{
name: 'Coveralls',
icon: 'https://coveralls.io/favicon.ico',
badge: 'https://img.shields.io/coveralls/:owner/:repo.svg',
url: 'https://coveralls.io/r/:owner/:repo'
},
{
name: 'Code Climate',
icon: 'https://codeclimate.com/favicon.ico',
badge: 'https://codeclimate.com/github/:owner/:repo.svg',
url: 'https://codeclimate.com/github/:owner/:repo'
},
{
name: 'Github Omnibox',
icon: 'tools',
url: chrome.extension.getURL('help.html')
}
];
var page = new Decorator();
if (page.isPublic()) {
page.addUrl();
chrome.storage.sync.get({menu:menu}, function(data){
page.addMenu(data.menu);
});
}