Last active
January 25, 2019 21:24
-
-
Save johnpapa/cf59b022b55433f82158e63394d9244f to your computer and use it in GitHub Desktop.
link generator for bookmarklet
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
// Directions | |
// Step 1: Click Gist Raw, then Copy/Paste entire gist into Bookmarklets.org/maker/ | |
// Step 2: Change the first 3 variables: alias, event, docs | |
// Step 3: At the bottom of the Bookmarklets.org page, right click the Bookmarklet Hyperlink and "copy link" | |
// Step 4: Right click int he bookmark bar in the browser and paste | |
// | |
// Credit: Thanks to Shayne Boyer and Scott Care for core code and inspiration | |
javascript: | |
( | |
function () { | |
var alias = 'jopapa'; | |
var event = prompt('Enter value for the event', ''); | |
var channel = prompt('Enter value for the channel', 'twitter'); | |
function copyToClipboard(text) { | |
var textarea = document.createElement("textarea"); | |
textarea.textContent = text; | |
textarea.style.position = "fixed"; /* Prevent scrolling to bottom of page in MS Edge.*/ | |
document.body.appendChild(textarea); | |
textarea.select(); | |
try { | |
return document.execCommand("copy"); /* Security exception may be thrown by some browsers.*/ | |
} catch (ex) { | |
console.warn("Copy to clipboard failed.", ex); | |
return false; | |
} finally { | |
document.body.removeChild(textarea); | |
} | |
} | |
//Get current URL | |
var baseUrl = document.location.href; | |
//remove current locale for sharing | |
const regex = /microsoft.com\/\w{2}-\w{2}\//g; | |
const subst = 'microsoft.com/'; | |
baseUrl = baseUrl.replace(regex,subst); | |
//respect or ignore currect query string | |
var separator = baseUrl.indexOf('?') > 0 ? '&' : '?'; | |
//respect or ignore hash | |
var hash = ''; | |
var hasHash = baseUrl.indexOf('#'); | |
if (hasHash != -1) { | |
hash = baseUrl.substr(hasHash); | |
baseUrl = baseUrl.replace(hash, ''); | |
} | |
//build final URL | |
var final = baseUrl + separator + 'WT.mc_id=' + event + '-' + channel + '-' + alias + hash; | |
copyToClipboard(final); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment