巴哈編輯器圖片網址格式化

在你要貼上 Twitter 跟 Discord 圖片連結時,改成巴哈能認出來的格式

目前為 2023-12-18 提交的版本,檢視 最新版本

// ==UserScript==
// @name         巴哈編輯器圖片網址格式化
// @namespace    https://home.gamer.com.tw/homeindex.php?owner=qwert535286
// @version      0.0.1
// @description  在你要貼上 Twitter 跟 Discord 圖片連結時,改成巴哈能認出來的格式
// @author       笑翠鳥
// @icon         https://www.google.com/s2/favicons?domain=gamer.com.tw
// @match        https://forum.gamer.com.tw/C.php?*
// @match        https://forum.gamer.com.tw/Co.php?*
// @match        https://forum.gamer.com.tw/post1.php?*
// @license      MIT
// ==/UserScript==

(() => {
	window.addEventListener('load', () => {
		const $iframeEditor = document.getElementById('editor').contentWindow.document.body

		$iframeEditor.addEventListener('paste', e => {
			const clipboardTxt = e.clipboardData.getData('text')

			let fmtTxt = ''
			switch(true) {
				case /cdn\.discordapp\.com\/attachments\/.*\.(jpg|jpeg|png|gif)\?/.test(clipboardTxt):
					fmtTxt = clipboardTxt.split('?')[0]
					break
				case /pbs\.twimg\.com\/media\/[a-zA-Z0-9\-\_]+\?format=(jpg|jpeg|png|webp)/.test(clipboardTxt):
					fmtTxt = clipboardTxt.split('&')[0].replace('?format=', '.').replace('webp', 'jpg')
					break
			}

			if (fmtTxt) {
				const targetCtx = `<a href="${clipboardTxt}">${clipboardTxt}</a>`.replace(/&/g, '&amp;')
				$iframeEditor.innerHTML = $iframeEditor.innerHTML.replace(targetCtx, `<img src="${fmtTxt}">`)
			}
		})
	})
})()

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址