DecentM / No native notifications

// ==UserScript==
// @name         No native notifications
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Converts native notifications into html ones, so they show up inside the site that created them instead of the desktop
// @author       DecentM
// @include      /^https?\:\/\/.*/
// @grant        none
// ==/UserScript==

window.returnFileExtension = function (string) {
        // This function returns the file extension of the passed string

        var re = /(?:\.([^.]+))?$/;
        return re.exec(string);
        // The returned object's [0] will be including the dot, [1] won't
};

window.loadExternalMedia = function (url) {
    return new Promise(function (fulfill, reject) {
        // Construct link in memory
        var file, domElement;

        switch(window.returnFileExtension(url)[1]) {
            case "css":
                domElement = document.getElementsByTagName('head')[0];
                file = document.createElement('link');
                file.type = "text/css";
                file.rel = "stylesheet";
                file.href = url;
                break;
            case "js":
                domElement = document.getElementsByTagName('body')[0];
                file = document.createElement('script');
                file.type = "text/javascript";
                file.src = url;
                file.async = true;
                break;
            default:
                reject(url + ": Refusing to load non css or js file");
        }

        // Assign success and fail responses for the promise
        file.onload = function () {
            fulfill(url);
        };
        file.onerror = function () {
            reject(url);
        };
        domElement.appendChild(file);
    });
};

window.loadMediaArray = function (array, callback) {
    if (typeof array === "undefined") {
        throw new Error("Nothing to load");
    }
    var i;
    var count;
    if (array.length > 0) {
        count = array.length;
    } else {
        return;
    }

    if (array.length !== 0) {
        for (i = 0; i < count; i++) {
            console.log("Pushing " + array[i] + " into promise");
            array.push(loadExternalMedia(array[i]));

            if (i > 1000) {
                throw new Error("Iteration exceeded " + i);
            }
        }
    } else {
        throw new Error("Array size is " + array.length);
    }

    Promise.all(array)
        .then(function () {
        if (typeof callback === "function") {
            callback();
        }}
             )
        .catch(function (error) {
        throw new Error("Promise error: " + error);
    });
};

window.replaceNotifications = function () {
    'use strict';
    console.log("Replacing window.Notification");

    window.oNotification = window.Notification;

    window.Notification = function () {
        console.log("Coverting notification:");
        console.log(arguments[0] + '\n' + '=----------------------------=' + '\n' + arguments[1].body);
        notyfy({
            text: arguments[1].body,
            layout: 'bottomRight',
            template: '<div class="notyfy_message"><h3 style="margin: 0; margin-top: 10px; margin-bottom: 10px">' + arguments[0] + '</h3><span class="notyfy_text"></span><div class="notyfy_close"></div></div>'
        });
        /*return new window.oNotification(arguments[0], arguments[1]);*/ //remove the comment tags if you want to show the notification
    };

    window.Notification.requestPermission = window.oNotification.requestPermission;

    window.oNotification = null;
};

window.loadMediaArray([
    "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"
], function () {
    window.loadMediaArray ([
        "http://craga89.github.io/notyfy/notyfy/jquery.notyfy.css",
        "http://craga89.github.io/notyfy/notyfy/themes/default.css",
        "http://craga89.github.io/notyfy/notyfy/jquery.notyfy.js"
    ], function () {
        window.replaceNotifications();
    });
});