forked from alexcpendleton/GithubForkConfirmation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGithubForkConfirmation.user.js
More file actions
73 lines (71 loc) · 2.46 KB
/
Copy pathGithubForkConfirmation.user.js
File metadata and controls
73 lines (71 loc) · 2.46 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
// ==UserScript==
// @name Github Fork Confirmation
// @namespace https://alexcpendleton.com/
// @version 1.0.1
// @description Adds a Confirm dialog to Github's Fork button
// @match https://github.com/*
// @match https://gist.github.com/*
// @include https://github.com/*
// @include https://gist.github.com/*
// @copyright 2014+, Alex Pendleton
// ==/UserScript==
(function () {
function GithubForkConfirmer(messageSource) {
var gfcAttributeName = "githubForkConfirmer";
var gfcAttributeValue = "true"
function isGist() {
return window.location.toString().toLowerCase().indexOf("https://gist.github") > -1;
}
function createConfirmationMessage() {
var messageName = isGist() ? "ConfirmGist" : "ConfirmProject";
return messageSource.getMessage(messageName);
}
function onFork(event) {
var result = confirm(createConfirmationMessage());
if (!result) {
event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation();
return false;
}
return true;
}
function findAndSetBindings() {
// When going to a Gist from the "All Gists" page, this script is never reloaded because it's using the History API or something
// From a content script we don't have access to history events (I couldn't find them anyway)
// So instead we wait for any click event on the page and then look for the fork button and subscribe to its click event
document.addEventListener("click", function(event) {
var selector = "form[action*='/fork'] button[type='submit']";
var forkButton = document.querySelector(selector);
if(forkButton) {
var hasAttribute = forkButton.getAttribute(gfcAttributeName);
if(!hasAttribute) {
forkButton.setAttribute(gfcAttributeName, gfcAttributeValue);
forkButton.addEventListener("click", onFork);
}
}
}, true);
}
this.bind = function() {
findAndSetBindings();
}
}
function MessageProvider() {
this.getMessage = function(name) {
var map = {
"ConfirmProject": {
"message":"Are you positive you want to fork this project?"
,"description":"Confirmation text for forking a project."
}
,"ConfirmGist": {
"message":"Are you positive you want to fork this Gist?"
,"description":"Confirmation text for forking a Gist."
}
};
var message = map[name];
return message ? message.message : "";
};
}
var confirmer = new GithubForkConfirmer(new MessageProvider());
confirmer.bind();
})();