allrecipes print version

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

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

function processClickEvent(ev) {
    "use strict";
    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;
    }
}

(function() {
    "use strict";
    document.addEventListener("click", processClickEvent, true);       // Mouse Button 1
    document.addEventListener("contextmenu", processClickEvent, true); // Mouse Button 2
    document.addEventListener("auxclick", processClickEvent, true);    // Mouse Button Middle
})();