// ==UserScript== // @name hipda-GIFs // @namespace https://github.com/maltoze/tampermonkey-scripts/ // @version 0.1.3 // @description GIFs support for HiPDA // @author maltoze // @match https://www.hi-pda.com/forum/* // @match https://www.4d4y.com/forum/* // @require https://cdn.jsdelivr.net/npm/@popperjs/core@2/dist/umd/popper.min.js // @require https://cdn.jsdelivr.net/npm/tippy.js@6/dist/tippy-bundle.umd.min.js // @require https://cdn.jsdelivr.net/gh/maltoze/tampermonkey-scripts@dbaa6eef6a1231c7bf30118d62daa38e052042c1/hipda/dist/hipda-giphy.umd.min.js // @resource lightBorderCss https://cdn.jsdelivr.net/npm/tippy.js@6/themes/light-border.css // @grant GM_getResourceText // @grant GM_addStyle // @license MIT // @downloadURL none // ==/UserScript== (function () { 'use strict'; const lastActionElem = document.querySelector( '#fastpostform > table > tbody > tr > td.postcontent > div > div > a:last-of-type', ); const cmdSmiliesElem = document.querySelector('#e_cmd_smilies'); if (!lastActionElem && !cmdSmiliesElem) { return; } const gifBtnOfPostBoxElem = document.createElement('a'); gifBtnOfPostBoxElem.style.padding = '2px 0 26px 0'; gifBtnOfPostBoxElem.style.cursor = 'pointer'; gifBtnOfPostBoxElem.style.background = 'none'; gifBtnOfPostBoxElem.innerHTML = ` GIF `; cmdSmiliesElem && cmdSmiliesElem.insertAdjacentElement('afterend', gifBtnOfPostBoxElem); const gifBtnElem = document.createElement('a'); gifBtnElem.style.textIndent = 0; gifBtnElem.style.textDecoration = 'none'; gifBtnElem.style.cursor = 'pointer'; gifBtnElem.style.width = '28px'; gifBtnElem.style.background = 'none'; gifBtnElem.style.display = 'flex'; gifBtnElem.style.alignItems = 'center'; gifBtnElem.innerHTML = ` `; lastActionElem && lastActionElem.insertAdjacentElement('afterend', gifBtnElem); const gifsContainerId = 'hipda-GIFs__container'; const gifsContainer = document.createElement('div'); gifsContainer.id = gifsContainerId; gifsContainer.innerHTML = `
`; const gifsContentId = 'hipda-GIFs__content'; const gifsContentElem = document.createElement('div'); gifsContentElem.id = gifsContentId; gifsContentElem.style.display = 'flex'; gifsContentElem.style.justifyContent = 'center'; gifsContentElem.style.overflowY = 'auto'; gifsContentElem.style.height = '350px'; gifsContentElem.style.width = '450px'; gifsContentElem.style.paddingTop = '0.5em'; gifsContainer.appendChild(gifsContentElem); gifsContainer.insertAdjacentHTML( 'beforeend', `
`, ); const gf = new hipdaGiphy.GiphyFetch('askdHCgTjMe0SVXAnUe15PICPgF1zlWh'); const searchGifs = (term) => (offset) => gf.search(term, { offset, limit: 10, lang: 'zh-CN' }); const fetchTrendingGifs = (offset) => { return gf.trending({ offset, limit: 10 }); }; const handleOnGifClick = (gif, e) => { e.preventDefault(); const fastPostElem = document.querySelector('#fastpostmessage'); const gifUrl = gif.images.original.url; if (fastPostElem) { const fastPostValue = fastPostElem.value; fastPostElem.value = ''.concat( fastPostValue.slice(0, fastPostElem.selectionStart), `[img]${gifUrl}[/img]`, fastPostValue.slice(fastPostElem.selectionEnd), ); } const postBoxIframe = document.querySelector('#postbox #e_iframe'); const postBoxBodyElem = postBoxIframe && postBoxIframe.contentWindow.document.querySelector('body'); if (postBoxBodyElem) { postBoxBodyElem.innerHTML += `${gif.title}`; } tippyInstance.hide(); }; const makeGrid = (fetchFunc, key = 'trending') => { const remove = hipdaGiphy.renderGrid( { width: 450, fetchGifs: fetchFunc, columns: 3, gutter: 6, user: {}, key, hideAttribution: true, noResultsMessage: '无结果', onGifClick: handleOnGifClick, }, gifsContentElem, ); return { remove }; }; const debounce = (callback, wait) => { let timeout; return (...args) => { const context = this; clearTimeout(timeout); timeout = setTimeout(() => callback.apply(context, args), wait); }; }; const handleOnInput = (event) => { const inputVal = event.target.value; makeGrid(searchGifs(inputVal), inputVal); }; const handleOnMount = () => { const searchInputElem = document.querySelector('#hipda-GIFs__search'); searchInputElem && searchInputElem.addEventListener('input', debounce(handleOnInput, 500)); makeGrid(fetchTrendingGifs); }; const tippyTheme = GM_getResourceText('lightBorderCss'); GM_addStyle(tippyTheme); const tippyTargetElem = lastActionElem ? gifBtnElem : gifBtnOfPostBoxElem; const tippyInstance = tippy(tippyTargetElem, { content: gifsContainer, trigger: 'click', interactive: true, appendTo: document.body, placement: 'auto', arrow: false, theme: 'light-border', maxWidth: 500, onMount: handleOnMount, }); })();