allrecipes print version

Skips to the print version of any recipe on allrecipes.com

目前為 2017-02-03 提交的版本,檢視 最新版本

// ==UserScript==
// @name        allrecipes print version
// @namespace   n/a
// @version     1
// @description Skips to the print version of any recipe on allrecipes.com
// @author      Greasy.Fork.User
// @match       allrecipes.com/*
// @grant       none
// ==/UserScript==

(function() {
    'use strict';
    document.addEventListener("click", function(ev) {
        var ele = ev.target;
        //check if the clicked element is within an "A" element
        while (ele.tagName !== "BODY") {
            if (ele.tagName === "A") {
                //make sure it links to a recipe page and it's not already pointing to a printable page
                if ((/^\/recipe\/\d+\//i).test(ele.pathname) && !(/^\/recipe\/\d+\/.*?\/print/i).test(ele.pathname)) {
                    //change the URL path to point to the printable version of the page
                    if (ele.pathname[ele.pathname.length-1] === "/") {
                        ele.pathname += "print/";
                    } else {
                        ele.pathname += "/print/";
                    }
                }
                break; //"A" element is already found. stop looking further
            }
            ele = ele.parentNode;
        }
    }, true);
})();