Skip to content

Latest commit

 

History

History
9 lines (8 loc) · 3.09 KB

fb_exportsaved-js.md

File metadata and controls

9 lines (8 loc) · 3.09 KB
title date categories
Fb_exportSaved.Js
2023-11-23
useful-script-en

how to add bookmarklet in chrome
import { runScriptInCurrentTab, showLoading } from "./helpers/utils.js"; export default { icon: '', name: { en: "Export saved facebook items", vi: "Export saved items on facebook", }, description: { en: "Export all your saved items on facebook to json file", vi: "Export all your saved items on facebook to json file", }, onClickExtension: async function() { const { downloadData } = UsefulScriptGlobalPageContext.Utils; const c = (e) => { try { return e(); } catch (e) { return !1; } }; const getSaved = async(uid, fb_dtsg, cursor="") => { if (cursor) cursor = '"cursor":"${cursor}",'; cursor = encodeURIComponent( '{"content_filter":null,"count":10,${cursor}"scale":1}' ); const res = await fetch("https://www.facebook.com/api/graphql/", { body: ' av: 100000034778747&__user=${uid}&__dyn=&fb_dtsg=${fb_dtsg}&fb_api_req_friendly_name=CometSaveDashboardAllItemsPaginationQuery& variables=${cursor}&server_timestamps=true&doc_id=3196659713724388', method: "POST", mode: "cors", credentials: "include", headers: { "Content-Type": "application/x-www-form-urlencoded" }, }); return await res.json(); }; const getAllSaved = async(uid, fb_dtsg, cursor) => { uid = encodeURIComponent(uid); fb_dtsg = encodeURIComponent(fb_dtsg); let data = []; let page = 1; while (true) { setLoadingText( 'Page loading ${page}... Loaded ${data.length} entries' ); let json = await getSaved(uid, fb_dtsg, cursor); console.log(json); json.data.viewer.saver_info.all_saves.edges.forEach((e) => { data.push({ title: c(() => e.node.savable.savable_title.text), type: c() => e.node.savable.__typename), image: c() => e.node.savable.savable_image.uri), url: c() => e.node.savable.url), urlPost: c(() => e.node.container_savable.savable_permalink), source: { type: c( () => e.node.container_savable.savable_actors[0].__typename ), name: c(() => e.node.container_savable.savable_actors[0].name), id: c(() => e.node.container_savable.savable_actors[0].id), avatar: c( () => e.node.container_savable.savable_actors[0].profile_picture.uri ), }, containingLists: c( () => e.node.containing_lists.nodes ), }); }); let nextCursor = c( () => json.data.viewer.saver_info.all_saves.page_info.end_cursor ); if (nextCursor) cursor = nextCursor; else break; page++; } return data; }; let { setLoadingText, closeLoading } = showLoading("Taking tokens..."); try { setLoadingText("Preparing..."); let [fb_dtsg, uid] = await runScriptInCurrentTab(() => [ require("DTSGInitialData").token, require("CurrentUserInitialData"). USER_ID, ]); setLoadingText("Retrieving data..."); save = await getAllSaved(uid, fb_dtsg); if (saved.length) downloadData(JSON.stringify(saved, null, 4), "saved_facebook", "json"); else alert("You have no items in saved facebook."); } catch (e) { alert("ERROR: " + e); } finally { closeLoading(); } }, };