Forked from spboyer/Bookmarklet for CDA Web Trends
Last active
July 17, 2018 15:02
-
-
Save MaximRouiller/61a5c594617b72165550a172aa28d622 to your computer and use it in GitHub Desktop.
Bookmarklet CDA Linker
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, drag the Bookmarklet Hyperlink to your Browser ToolBar | |
// Step 4: Enjoy | |
javascript: | |
( | |
function () { | |
var alias = 'marouill'; | |
var event = prompt('Enter value for the event', 'social'); | |
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