-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontributing.user.js
116 lines (98 loc) · 3.99 KB
/
contributing.user.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
// ==UserScript==
// @name GitHub CONTRIBUTING includer
// @namespace https://github.com/ghes
// @version 0.1.2
// @description Include CONTRIBUTING.md in places you'd contribute
// @author Stuart P. Bentley (@stuartpb)
// @match https://github.com/*/*/issues/new
// @match https://github.com/*/*/compare/*
// @match https://github.com/*/*/new/*
// @match https://github.com/*/*/edit/*
// @match https://github.com/*/*/delete/*
// @grant none
// ==/UserScript==
// Copyright 2015 Stuart P. Bentley.
// This work may be used freely as long as this notice is included.
// The work is provided "as is" without warranty, express or implied.
function createClearfix () {
var cf = document.createElement('div');
cf.className = 'meta clearfix';
return cf;
}
function getContributingLink(doc){
var message = doc.getElementsByClassName('contributing')[0];
return message && message.getElementsByTagName('a')[0];
}
function importContributing(resDoc,href) {
var readme = resDoc.getElementById('readme');
if (readme) {
readme.id = 'contributing';
readme.className =
'readme boxed-group clearfix announce instapaper_body md';
document.adoptNode(readme);
var h3 = document.createElement('h3');
var namespan = document.createElement('a');
namespan.href = href;
namespan.textContent = ' '+href.slice(href.lastIndexOf('/')+1);
var icon = document.createElement('span');
icon.className = 'octicon octicon-megaphone';
namespan.insertBefore(icon, namespan.firstChild);
h3.appendChild(namespan);
readme.insertBefore(h3, readme.firstChild);
// This is generally the best container to add to for all the pages
// we transclude the README into.
var container = document.getElementById('js-repo-pjax-container')
.querySelector('.repository-content');
// Some forms' last element give enough space, some don't
// I don't if there'd be a better class to put on the element
// or something like that, so we'll just hack it
readme.style.marginTop = '15px';
container.appendChild(createClearfix());
container.appendChild(readme);
}
}
function getDocument(href,cb) {
var request = new XMLHttpRequest();
request.addEventListener('load', function(){
cb(request.response);
});
request.open('GET',href,true);
request.responseType = 'document';
request.send();
}
var link = getContributingLink(document);
if (link) {
getDocument(link.href, function(resDoc){
importContributing(resDoc, link.href);
// Change the warning to point to the new location
link.href = '#contributing';
// since it's taking them somewhere on this page, remove target=_blank
link.removeAttribute('target');
});
} else {
// Editor pages don't have a contributing message that would point us to the
// CONTRIBUTING file, by default, so we cheat by stealing it from the New
// Issue page. If the repo doesn't accept issues, this'll just have to fail.
// (Enforce route because /compare/ can be missing the "contributing"
// message, in cases where we don't want it.)
var base = /^(https:\/\/github\.com\/[^/]+\/[^/]+)\/(new|edit|delete)/
.exec(location.href);
if (base){
base = base[1];
getDocument( base + '/issues/new', function(linkResDoc) {
// We could get the whole Contributing message here, adopt the div,
// and add it above the form here, but we don't, for these reasons:
// 1. That would push existing elements down, and interfering with click
// targets after page load without user interaction is *the worst*.
// 2. Users don't necessarily *want* their change to go through the pull
// request system, so there's no need to boink them about it yet.
link = getContributingLink(linkResDoc);
if (link) {
getDocument(link.href, function(readmeResDoc){
importContributing(readmeResDoc, link.href);
// no need to update the link since we're not importing it
});
}
});
}
}