Last active
June 19, 2018 13:11
-
-
Save bitmvr/9c4f63e34539cda784f9375f06443000 to your computer and use it in GitHub Desktop.
GitHub Branch Banner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript: (function() { | |
const getBody = function() { | |
return document.body; | |
}; | |
const createBannerDiv = function() { | |
const bannerDiv = document.createElement('div'); | |
bannerDiv.setAttribute('id', 'sfmc-tooling--banner'); | |
return bannerDiv; | |
}; | |
const createBannerText = function(text) { | |
const bannerText = document.createElement("INPUT"); | |
bannerText.setAttribute("type", "text"); | |
bannerText.setAttribute("value", text); | |
const styling = ` | |
background: none; | |
border: none; | |
outline: none; | |
color: #ffffff; | |
width: inherit; | |
text-align: center; | |
`; | |
bannerText.style.cssText = styling; | |
return bannerText | |
}; | |
const showBanner = function(banner) { | |
getBody().prepend(banner); | |
setTimeout(function() { | |
document.getElementById('sfmc-tooling--banner').remove(); | |
}, 2000); | |
}; | |
const createBanner = function(text) { | |
const banner = createBannerDiv(); | |
banner.append(); | |
banner.append(createBannerText(text)); | |
const styling = ` | |
position:sticky; | |
top: 0px; | |
z-index: 9999; | |
background-color: #009EDB; | |
color: #ffffff; | |
height: 50px; | |
line-height: 50px; | |
text-align: center; | |
width: 100%; | |
`; | |
banner.style.cssText = styling; | |
return showBanner(banner); | |
}; | |
const getIssue = function() { | |
const issue = document.querySelector('.js-issue-title') | |
.textContent | |
.trim() | |
.toLowerCase() | |
.replace(/[^A-Za-z0-9]/gi, ' ') | |
.replace(/\W/gi, '-'); | |
return issue | |
}; | |
const getGitHubIssueNum = function() { | |
const issueNum = document.querySelector('.gh-header-number') | |
.textContent | |
.trim() | |
.replace('#', ''); | |
return issueNum | |
}; | |
const isIssuePage = function() { | |
let currentURL = window.location.href; | |
const issuePageRegex = /.*\/issues\/\d*($)?(#issue.*)?/; | |
return currentURL.match(issuePageRegex) !== null; | |
}; | |
const isGitHub = function () { | |
const targetURL = window.location.href; | |
const targetRegex = /https:\/\/.*github(.exacttarget)?.com/; | |
return targetURL.match(targetRegex) !== null; | |
}; | |
if ( isGitHub() && isIssuePage() ) { | |
const branchName = 'feature/' + 'gh-' + getGitHubIssueNum() + '/' + getIssue(); | |
createBanner(branchName); | |
myBranch = document.querySelector('#sfmc-tooling--banner>input'); | |
myBranch.select(); | |
document.execCommand('copy'); | |
} else { | |
console.warn('Target URL does not appear to be an issue page on an instance of GitHub'); | |
}; | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment