Melonbooks: cart: save to file

Save cart to text file

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Melonbooks: cart: save to file
// @namespace    http://darkfader.net/
// @version      0.1
// @run-at       document-idle
// @description  Save cart to text file
// @author       Rafael Vuijk
// @match        http*://www.melonbooks.co.jp/clipboard/*
// @require      http://code.jquery.com/jquery.min.js
// @grant        GM_openInTab
// @grant        GM_registerMenuCommand
// @grant        GM_download
// ==/UserScript==

(function() {
    'use strict';

    function save_cart() {
        //var file = new File(["Hello, world!"], "melonbooks_cart.txt", {type: "text/plain;charset=utf-8"});
        //saveAs(file);//blob, "cart.txt");

        let str = '';

        $("table[class='list mb20']").find("tr").each(function () {
            str += $(this).find("td[class='name']").find("a").text() + '\n';
        });

        // let test = 'data:text/plain;charset=utf-8;base64,' + btoa(str));
        let test = 'data:text/plain;charset=utf-8,' + encodeURIComponent(str);

        GM_download({
            url: test,
            name: "melonbooks_cart.txt",
            headers: {}, // - see GM_xmlhttpRequest for more details
            saveAs: false, // - boolean value, show a saveAs dialog
            onerror: () => { console.log('error'); },
            onload: () => { console.log('finished'); },
            onprogress: () => { console.log('progress'); },
            ontimeout: () => { console.log('timeout'); },
        });
    }

    GM_registerMenuCommand("save cart (delete first!)", save_cart, "s");

//    save_cart();

    console.log('---------------------');

    $("table[class='list mb20']").find("tr").each(function () {
        var title = $(this).find("td[class='name']").find("a").text();
        title = title.replace(/【メロン.*】/, "");
        console.log(title);
        let link = function() {
            var td = $('<td/>');
            // insertAfter, wrap etc..
            var link = $('<a/>');
            link.attr('target', '_blank');
            link.attr('href', 'https://www.suruga-ya.jp/search?searchbox=1&category=11&search_word=' + title);
            link.text('suru');
            td.append(link);
            return td;
        };
        let img = $(this).find("td[class='img']");
        link().insertAfter(img);
    });

})();