外卖菜单转换

获取美团/饿了么/百度外卖菜单页并指定格式输出

当前为 2016-12-16 提交的版本,查看 最新版本

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @namespace    undefined
// @version      1.0.0

// @name         外卖菜单转换
// @description  获取美团/饿了么/百度外卖菜单页并指定格式输出

// @run-at       document-end
// @include      *://waimai.meituan.com/restaurant/*
// @include      *://i.waimai.meituan.com/restaurant/*
// @include      *://waimai.baidu.com/waimai/shop/*
// @include      *://client.waimai.baidu.com/mobile/waimai*
// @include      *://h5.ele.me/shop/*
// @include      *://www.ele.me/shop/*

// @author       freeshine<[email protected]>

// @copyright  2016
// ==/UserScript==

/**
 * 美团外卖PC版
 */
function meituan() {
    const {
        btn,
        textarea
    } = createComponent('美团外卖菜单PC版');

    btn.addEventListener('click', getMeituanMenu.bind(this, btn, textarea), false)
}

function getMeituanMenu(btn, textarea) {
    let info = '';
    const category = document.querySelectorAll('.food-nav .category');
    for (let i = 0, categoryLength = category.length; i < categoryLength; i++) {
        const foodTypeName = category[i].querySelector('.title > span').textContent.trim();
        info += "#" + foodTypeName + "\n";

        const item = category[i].querySelectorAll('.pic-food');
        for (let j = 0, itemLength = item.length; j < itemLength; j++) {
            const foodName = item[j].querySelector('.np .name').getAttribute('title').trim();
            const foodPrice = item[j].querySelector('.labels .price .only').textContent.split('/')[0].substring(1).trim();
            info += assembleItem(foodName, foodPrice);
            info = info.replace('起', '');
        }
    }
    textarea.value = info;
}

/**
 * 美团外卖手机版
 */
function meituanMobile() {
    const {
        btn,
        textarea
    } = createComponent('美团外卖菜单手机版');

    btn.addEventListener('click', getMeituanMobileMenu.bind(this, btn, textarea), false)
}

function getMeituanMobileMenu(btn, textarea) {
    let info = '';
    const category = document.querySelectorAll('.foodlistwrap .foodlist');
    for (let i = 0, categoryLength = category.length; i < categoryLength; i++) {
        const foodTypeName = category[i].querySelector('.foodlist-label').textContent.trim();
        info += "#" + foodTypeName + "\n";

        const item = category[i].querySelectorAll('.fooditem');
        for (let j = 0, itemLength = item.length; j < itemLength; j++) {
            const foodName = item[j].getAttribute('data-foodname').trim();
            const foodPrice = item[j].getAttribute('data-price').trim();
            info += assembleItem(foodName, foodPrice);
        }
    }
    info = info.replace('APP下单优惠菜品', '折扣');
    textarea.value = info;
}

/**
 * 百度外卖PC版
 */
function baidu() {
    const {
        btn,
        textarea
    } = createComponent('百度外卖菜单PC版');

    btn.addEventListener('click', getBaiduMenu.bind(this, btn, textarea), false)
}

function getBaiduMenu(btn, textarea) {
    let info = '';
    const category = document.querySelectorAll('.menu-list .list-wrap');
    for (let i = 0, categoryLength = category.length; i < categoryLength; i++) {
        const foodTypeName = category[i].querySelector('.list-status .title').textContent.trim();
        info += "#" + foodTypeName + "\n";

        const item = category[i].querySelectorAll('.list-item');
        for (let j = 0, itemLength = item.length; j < itemLength; j++) {
            const foodName = item[j].querySelector('h3').getAttribute('data-title').trim();
            const foodPrice = item[j].querySelector('.m-price del') ? item[j].querySelector('.m-price del strong').textContent : item[j].querySelector('.m-price strong').textContent;
            info += assembleItem(foodName, foodPrice.substring(1).trim());
        }
    }
    textarea.value = info;
}

/**
 * 百度外卖手机版
 */
function baiduMobile() {
    const {
        btn,
        textarea
    } = createComponent('百度外卖菜单手机版');

    btn.addEventListener('click', getBaiduMobileMenu.bind(this, btn, textarea), false)
}

function getBaiduMobileMenu(btn, textarea) {
    let info = '';
    const category = document.querySelectorAll('#shopmenu-category .list-item a');
    const categoryGroup = document.querySelectorAll('#shopmenu-list .listgroup')

    for (let i = 0, categoryLength = category.length; i < categoryLength; i++) {

        const foodTypeName = category[i].textContent.trim();
        info += "#" + foodTypeName + "\n";

        const item = categoryGroup[i].querySelectorAll('.list-item');
        for (let j = 0, itemLength = item.length; j < itemLength; j++) {
            const foodName = item[j].querySelector('.title').textContent.trim();
            const foodPrice = item[j].querySelector('.price').textContent.substring(1).trim();
            info += assembleItem(foodName, foodPrice);
        }
    }
    textarea.value = info;
}

/**
 * 饿了么PC版
 */
function eleme() {
    const {
        btn,
        textarea
    } = createComponent('饿了么菜单PC版');

    btn.addEventListener('click', getElemeMenu.bind(this, btn, textarea), false)
}

function getElemeMenu(btn, textarea) {
    let info = '';
    const category = document.querySelectorAll('.shopmenu-main .shopmenu-list');
    for (let i = 0, categoryLength = category.length; i < categoryLength; i++) {
        const foodTypeName = category[i].querySelector('.shopmenu-title').textContent.split(' ')[0].trim();
        info += "#" + foodTypeName + "\n";

        const item = category[i].querySelectorAll('.shopmenu-food');
        for (let j = 0, itemLength = item.length; j < itemLength; j++) {
            const foodName = item[j].querySelector('.shopmenu-food-name').textContent.trim();
            const foodPrice = item[j].querySelector('.shopmenu-food-price').textContent.trim();
            info += assembleItem(foodName, foodPrice);
        }
    }
    textarea.value = info;
}

/**
 * 饿了么手机版
 */
function elemeMobile() {
    const {
        btn,
        textarea
    } = createComponent('饿了么菜单手机版');

    btn.addEventListener('click', getElemeMobileMenu.bind(this, btn, textarea), false)
}

function getElemeMobileMenu(btn, textarea) {
    let info = '';
    const category = document.querySelectorAll('.menu-list dl');
    for (let i = 0, categoryLength = category.length; i < categoryLength; i++) {
        const foodTypeName = category[i].querySelector('dt strong').textContent.trim();
        info += "#" + foodTypeName + "\n";

        const item = category[i].querySelectorAll('dd');
        for (let j = 0, itemLength = item.length; j < itemLength; j++) {
            const foodName = item[j].querySelector('.foodtitle span').textContent.trim();
            const foodPrice = item[j].querySelector('.foodprice .foodprice-origin') ? item[j].querySelector('.foodprice .foodprice-origin').textContent.trim() : item[j].querySelector('.foodprice span').textContent.trim();
            info += assembleItem(foodName, foodPrice);
        }
    }
    textarea.value = info;
}

/**
 * 创建页面组件
 */
function createComponent(btnName) {
    console.info(btnName);
    const body = document.body;
    const btn = document.createElement('button');
    btn.style.cssText = "position:fixed;z-index:9999;left:0;bottom:400px;background:#fff;width:120px;height:40px;";
    btn.innerHTML = btnName;
    body.appendChild(btn);

    const textarea = document.createElement('textarea');
    textarea.style.cssText = "position:fixed;z-index:9999;left:0;bottom:0px;height:380px;";
    body.appendChild(textarea);
    return {
        btn,
        textarea
    };
}

/**
 * 按格式 组装name price
 */
function assembleItem(name, price) {
    return name + ' ' + price + '\n';
}

function init() {
    if (location.host === 'i.waimai.meituan.com') {
        meituanMobile();
    } else if (location.host === 'waimai.meituan.com') {
        meituan();
    } else if (location.host === 'client.waimai.baidu.com') {
        baiduMobile();
    } else if (location.host === 'waimai.baidu.com') {
        baidu();
    } else if (location.host === 'h5.ele.me') {
        elemeMobile();
    } else if (location.host === 'www.ele.me') {
        eleme();
    }
}

init();