RES Twitter Fix

Fixes RES Twitter expandos in Firefox

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name		RES Twitter Fix
// @namespace	com.res-twitter-fix
// @description	Fixes RES Twitter expandos in Firefox
// @match		https://*.reddit.com/*
// @run-at		document-end
// @grant		none
// @version		3.2
// ==/UserScript==

"use strict"

function injectTwitterScript()
{
	// inject twitter script to convert blockquotes into pretty twitter displays
	const script = document.createElement( 'script' )
	script.src = 'https://platform.twitter.com/widgets.js'
	document.body.appendChild( script )
}

function fixExpandos()
{
	// look for un-rendered twitter expandos and proceed if any are found
	console.log( 'looking for expandos' )
	const expandos = document.querySelectorAll( '.twitter-tweet:not(.twitter-tweet-rendered' )
	if ( expandos.length > 0 )
	{
		console.log( 'fixing expandos!' )
		injectTwitterScript()
	}
}

function onClick( e )
{
	// check if we've clicked an expando button
	if ( e.target.className.includes( 'expando-button' ) )
	{
		// fix expandos on click
		fixExpandos()

		// also attempt to fix auto-expanded expandos...
		// perhaps this is not the most reliable way to do it, as there's
		// no way to know exactly how long it'll take RES to auto-expand.
		// regardless, injecting the script twice does no harm
		setTimeout( () => { fixExpandos() }, 1000 )
	}
}
document.addEventListener( 'click', onClick, false )