Skip to content

Instantly share code, notes, and snippets.

@Jan-Zeiseweis
Created September 23, 2018 19:45
Show Gist options
  • Save Jan-Zeiseweis/d45a7206590d1f3577a51dae9517275e to your computer and use it in GitHub Desktop.
Save Jan-Zeiseweis/d45a7206590d1f3577a51dae9517275e to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Track Pageview
// @namespace deKexx
// @description Sends Url to a local server
// @include http://www.youtube.com/watch*
// @include https://www.youtube.com/watch*
// @connect popo.local
// @author Jan Zeiseweis
// @version 0.1
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function() {
'use strict';
var oldHref = location.href;
function track() {
var requestDetails = {
method: "POST",
url: "http://popo.local:5555/tracker/api/v1.0/pageview",
data: `{"url":"${oldHref}"}`,
headers: {
"Content-Type": "application/json"
},
onload: function(response) {
console.log(response.responseText);
}
}
console.log(requestDetails);
GM_xmlhttpRequest(requestDetails);
};
track();
// Track pages that don't really reload, but refresh the entire content (like youtube).
var mutationObserver = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (oldHref != document.location.href) {
oldHref = document.location.href;
track();
return true;
}
});
});
mutationObserver.observe(document.getElementById('movie_player'), {
attributes: false,
characterData: false,
childList: true,
subtree: true,
attributeOldValue: false,
characterDataOldValue: false
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment