Last active
February 8, 2021 09:11
-
-
Save unarist/9722831a794b5537dc0f749ee49da55a to your computer and use it in GitHub Desktop.
Mastodon - Add debug tools to window.mstdbg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Mastodon - Add debug tools to window.mstdbg | |
// @namespace https://github.com/unarist | |
// @version 0.5 | |
// @description no description :) | |
// @author unarist | |
// @match https://*/web/* | |
// @require https://unpkg.com/axios/dist/axios.min.js | |
// @grant none | |
// @downloadURL https://gist.github.com/unarist/9722831a794b5537dc0f749ee49da55a/raw/mastodon-mstdbg.user.js | |
// @run-at document-idle | |
// @noframes | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
if (!document.querySelector('#mastodon')) return; | |
const self = {}, | |
defGet = (prop, func) => Object.defineProperty(self, prop, { configurable: true, enumerable: true, get: func }), | |
defVal = (prop, value) => Object.defineProperty(self, prop, { configurable: true, enumerable: true, value, writable: false }); | |
// returned function can be used as... | |
// * function to get deep-converted object | |
// * getter properties to get deep-convertion of specific item | |
const toLazyMap = (map) => Object.defineProperties(() => map.toJS(), map.map(v => ({ enumerable: true, get: () => v.toJS() })).toObject()); | |
const findProp = (name) => { | |
// >= v16: to descendant | |
const rootContainer = (typeof unsafeWindow !== 'undefined' ? unsafeWindow : window).document.querySelector('#mastodon')._reactRootContainer; | |
let current_node = (rootContainer._internalRoot /* v16.3 */ || rootContainer).current.child; | |
while (current_node) { | |
const obj = current_node.memoizedProps[name]; | |
if (obj) return obj; | |
current_node = current_node.child; | |
} | |
}; | |
defGet('initialState', () => JSON.parse(document.getElementById('initial-state').textContent)); | |
defGet('api', () => axios.create({ | |
headers: { Authorization: `Bearer ${self.initialState.meta.access_token}` }, | |
transformResponse: [function (data) { try { return JSON.parse(data); } catch(Exception) { return data; } }] | |
})); | |
defGet('store', () => findProp('store')); | |
defGet('state', () => self.store.getState()); | |
defGet('stateMap', () => toLazyMap(self.store.getState())); | |
defGet('history', () => findProp('history')); | |
defGet('statuses', () => toLazyMap(self.state.get('statuses'))); | |
defGet('accounts', () => toLazyMap(self.state.get('accounts'))); | |
defGet('timelines', () => toLazyMap(self.state.get('timelines'))); | |
defGet('currentEntity', () => self.state.getIn(location.href.match(/(accounts|statuses)\/(\d+)/).slice(1)).toJS()); | |
defGet('currentEntityJson', () => self.api.get('/api/v1/' + location.href.replace(/.+\/(accounts|statuses)\/(\d+).*/, '$1/$2'))); | |
defGet('currentTimeline', () => self.state.getIn( | |
location.href | |
.replace('/public/local', '/community') | |
.replace('/tag/', '/hashtag/') | |
.replace('/accounts/', '/timelines/account/') | |
.replace(/\//g, ':') | |
.match(/:(timelines):(.+)/) | |
.slice(1) | |
).toJS()); | |
window.mstdbg = self; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment