// ==UserScript==
// @name Hooktube hook
// @namespace 4e956ccc9cac9f46a99dbe44ae3d8fff0431d442
// @version 0.1
// @description Replaces youtube Video links with hooktube video links if on the search page
// @author /u/AyrA_ch
// @match https://www.youtube.com/*
// @grant none
// @external true
// ==/UserScript==
//Changelog
//0.1 - initial version
(function () {
'use strict';
var filter1 = /(?:youtu\.be\/|youtube(-nocookie)?.com\/(?:v\/|e\/|.*u\/\w+\/|embed\/|.*v=))([\w\-]{11})/;
var filter2 = /^\/watch[\?\&]v=([\w\-]{11})/;
var schedule = null;
//Swap youtube links
var swapLinks = function () {
var links = document.querySelectorAll("a[href]");
var m;
for (var i = 0; i < links.length; i++) {
m = links[i].href.match(filter1);
if (!m) {
m = links[i].href.match(filter2);
}
if (m) {
links[i].href = "https://hooktube.com/watch?v=" + m[m.length - 1];
}
}
schedule = null;
};
//Plan a link swap to not overuse the DOM event
var timeSwap = function () {
//only when on search page
if (location.pathname == "/results") {
if (!schedule) {
schedule = window.setTimeout(swapLinks, 200);
}
}
};
document.body.addEventListener('DOMSubtreeModified', function () {
//Do not update immediately, otherwise we run into an infinite loop.
timeSwap();
}, false);
swapLinks();
})();
/*
LICENSE:
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
The full license text can be found here: http://creativecommons.org/licenses/by-nc-sa/4.0/
The link has an easy to understand version of the license and the full license text.
DISCLAIMER:
THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/