Skip to main content
Bounty Ended with 50 reputation awarded by Elements In Space
added 2175 characters in body
Source Link
cocomac
  • 14.3k
  • 6
  • 29
  • 84

Update: Unfortunately, I don't see an API endpoint for Saves. We could request one on stackapps, but there isn't one now. Still, it's possible to fetch the saves by fetching the entire Saves page and parsing the DOM, and I show that below.

Warning: this fetches the DOM every time you load a page. A better approach is probably to cache it somehow, but this is a decent starting point if I (or someone else) finish it later.

// ==UserScript==
// @name         Saves Fetcher
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Script to fetch your saves list on MSE on questions pages
// @author       cocomac
// @match        https://meta.stackexchange.com/questions/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=stackexchange.com
// @grant        none
// ==/UserScript==

(() => {
    'use strict';
    $(() => {
        // figure out the user ID of the current user
        let uid = document.getElementsByClassName("s-user-card__small")[0].href.split('/')[4];

        // fetch the saves list for current user
        // sadly, API endpoint doesn't exist - fetch the whole page
        $.get(`https://meta.stackexchange.com/users/saves/${uid}`, (res) => {
            // parse the DOM
            let dom = new DOMParser().parseFromString(res, "text/html");

            // find the saves elements
            let targets = Array.from(dom.getElementsByClassName("js-saves-post-summary"));
            let savesNotes = [];
            targets.forEach((elem) => {
                // if it's a save that actually has a note attached
                if ($.trim(elem.children[1].children[4].outerText).length !== 0) {
                    // add it to the list
                    savesNotes.push(
                        {
                            "id": elem.id.split("-")[2],
                            "note": elem.children[1].children[4].outerText
                        }
                    )
                }
            });

            // log the list to the console
            console.log(savesNotes);
        });
    });
})();


Update: Unfortunately, I don't see an API endpoint for Saves. We could request one on stackapps, but there isn't one now. Still, it's possible to fetch the saves by fetching the entire Saves page and parsing the DOM, and I show that below.

Warning: this fetches the DOM every time you load a page. A better approach is probably to cache it somehow, but this is a decent starting point if I (or someone else) finish it later.

// ==UserScript==
// @name         Saves Fetcher
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Script to fetch your saves list on MSE on questions pages
// @author       cocomac
// @match        https://meta.stackexchange.com/questions/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=stackexchange.com
// @grant        none
// ==/UserScript==

(() => {
    'use strict';
    $(() => {
        // figure out the user ID of the current user
        let uid = document.getElementsByClassName("s-user-card__small")[0].href.split('/')[4];

        // fetch the saves list for current user
        // sadly, API endpoint doesn't exist - fetch the whole page
        $.get(`https://meta.stackexchange.com/users/saves/${uid}`, (res) => {
            // parse the DOM
            let dom = new DOMParser().parseFromString(res, "text/html");

            // find the saves elements
            let targets = Array.from(dom.getElementsByClassName("js-saves-post-summary"));
            let savesNotes = [];
            targets.forEach((elem) => {
                // if it's a save that actually has a note attached
                if ($.trim(elem.children[1].children[4].outerText).length !== 0) {
                    // add it to the list
                    savesNotes.push(
                        {
                            "id": elem.id.split("-")[2],
                            "note": elem.children[1].children[4].outerText
                        }
                    )
                }
            });

            // log the list to the console
            console.log(savesNotes);
        });
    });
})();

Source Link
cocomac
  • 14.3k
  • 6
  • 29
  • 84

I would support this feature request, but… with everything that is happening at SE right now, I don't know if this would get done soon.

That said, it might be possible to do this with a userscript. Unfortunately, I don't see an obvious API path for it. Regardless, it's likely possible by looking at how the "real" Saves page gets them, and make a similar API call.

Having this feature could be useful for me, so I might further investigate making a userscript for this when I have a chance (if so, I’ll update this).