-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub.com.user.js
More file actions
207 lines (166 loc) · 6.52 KB
/
github.com.user.js
File metadata and controls
207 lines (166 loc) · 6.52 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// ==UserScript==
// @name GitHub Quick Import
// @namespace au.benjithatfoxguy.github.com
// @version 0.3.0
// @description Adds an Import button on GitHub repos and pre-fills the import flow
// @author BenjiThatFoxGuy
// @match https://github.com/*
// @grant none
// @license MIT
// @updateURL https://updates.benjifox.gay/github.com.meta.js
// @downloadURL https://updates.benjifox.gay/github.com.user.js
// ==/UserScript==
(function() {
'use strict';
const IMPORT_PARAM = 'source_url';
const BUTTON_ID = 'gh-quick-import-btn';
function setReactInputValue(input, value) {
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(
window.HTMLInputElement.prototype,
'value'
).set;
nativeInputValueSetter.call(input, value);
const events = [
new Event('input', { bubbles: true }),
new Event('change', { bubbles: true }),
new Event('blur', { bubbles: true })
];
events.forEach(event => input.dispatchEvent(event));
}
function isRepositoryPage() {
const pathParts = location.pathname.split('/').filter(Boolean);
if (pathParts.length < 2) {
return false;
}
const reserved = new Set([
'about', 'account', 'codespaces', 'collections', 'contact', 'customer-stories',
'dashboard', 'enterprise', 'events', 'explore', 'features', 'gist', 'home',
'issues', 'login', 'marketplace', 'new', 'notifications', 'organizations',
'orgs', 'pricing', 'pulls', 'search', 'security', 'settings', 'signup', 'sponsors',
'team', 'topics', 'trending', 'users'
]);
return !reserved.has(pathParts[0]);
}
function buildRepoUrl() {
const pathParts = location.pathname.split('/').filter(Boolean);
return `${location.origin}/${pathParts[0]}/${pathParts[1]}`;
}
function createImportButton(repoUrl) {
const anchor = document.createElement('a');
anchor.id = BUTTON_ID;
anchor.className = 'btn btn-sm';
anchor.href = `/new/import?${IMPORT_PARAM}=${encodeURIComponent(repoUrl)}`;
anchor.textContent = 'Import/Private Fork';
anchor.setAttribute('data-turbo', 'false');
anchor.setAttribute('aria-label', 'Import this repository into a new repository');
const li = document.createElement('li');
li.appendChild(anchor);
return li;
}
function findPageheadActions() {
return document.querySelector('ul.pagehead-actions, div.pagehead-actions ul, div.AppHeader-actions ul');
}
function insertButtonIntoActions() {
if (!isRepositoryPage()) {
return;
}
const actions = findPageheadActions();
if (!actions || actions.querySelector(`#${BUTTON_ID}`)) {
return;
}
const repoUrl = buildRepoUrl();
const newButtonLi = createImportButton(repoUrl);
const actionItems = Array.from(actions.children).filter((node) => node.tagName === 'LI');
const starItem = actionItems.find((item) => /\bStar\b/i.test(item.textContent || ''));
const forkItem = actionItems.find((item) => /\bFork\b/i.test(item.textContent || ''));
if (starItem && forkItem && starItem.nextSibling) {
actions.insertBefore(newButtonLi, forkItem);
return;
}
if (starItem && starItem.nextSibling) {
actions.insertBefore(newButtonLi, starItem.nextSibling);
return;
}
if (forkItem) {
actions.insertBefore(newButtonLi, forkItem);
return;
}
actions.appendChild(newButtonLi);
}
function findImportUrlInput() {
return document.querySelector(
'#_r_2_, input[name="vcs_url"], input[name*="vcs" i], input[type="url"]'
);
}
function getRepoNameFromUrl(sourceUrl) {
try {
const parsed = new URL(sourceUrl);
const parts = parsed.pathname.split('/').filter(Boolean);
if (parts.length < 2) {
return '';
}
return parts[1].replace(/\.git$/i, '');
} catch {
return '';
}
}
function findRepositoryNameInput() {
return document.querySelector(
'#repository-name-input, input[name="repository[name]"], input[name="repository_name"], input[aria-label*="Repository name" i]'
);
}
function findPrivateVisibilityControl() {
return document.querySelector(
'#_r_10_, input[name="repository[visibility]"][value="private"], input[name="visibility"][value="private"], input[type="radio"][value="private"]'
);
}
function fillImportUrlFromParam() {
if (location.pathname !== '/new/import') {
return;
}
const sourceUrl = new URLSearchParams(location.search).get(IMPORT_PARAM);
if (!sourceUrl) {
return;
}
const repoName = getRepoNameFromUrl(sourceUrl);
const tryFill = () => {
const importUrlInput = findImportUrlInput();
const repositoryNameInput = findRepositoryNameInput();
const privateVisibilityControl = findPrivateVisibilityControl();
if (!importUrlInput) {
return false;
}
if (importUrlInput.value !== sourceUrl) {
setReactInputValue(importUrlInput, sourceUrl);
}
if (repositoryNameInput && repoName && repositoryNameInput.value !== repoName) {
setReactInputValue(repositoryNameInput, repoName);
}
if (privateVisibilityControl && !privateVisibilityControl.checked) {
privateVisibilityControl.click();
privateVisibilityControl.dispatchEvent(new Event('input', { bubbles: true }));
privateVisibilityControl.dispatchEvent(new Event('change', { bubbles: true }));
}
return true;
};
if (tryFill()) {
return;
}
const observer = new MutationObserver(() => {
if (tryFill()) {
observer.disconnect();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
setTimeout(() => observer.disconnect(), 10000);
}
function run() {
insertButtonIntoActions();
fillImportUrlFromParam();
}
run();
document.addEventListener('turbo:load', run);
})();