allrecipes print version

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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
})();