// ==UserScript== // @name 小红书复制出#标签给微博用+下载高清图片 // @namespace http://tampermonkey.net/ // @version 0.2 // @description 小红书复制出#标签给微博用+下载高清图片! // @author You // @match https://www.xiaohongshu.com/explore/* // @match .xhscdn.com//format* // @icon https://www.google.com/s2/favicons?sz=64&domain=xiaohongshu.com // @grant GM_setClipboard // @grant GM_addStyle // @run-at document-end // ==/UserScript==

(function() { 'use strict'; GM_addStyle('.myBtn {cursor: pointer; background-color: #b4e40c; border-radius: 3px; color: black; padding: 5px; margin: 3px;}
#myfloat { position: fixed !important; top: 132px; right: 8px; padding: 5px; z-index: 100; background-color: #C3ECBF;}
'); // unsafeWindow.copyOutTag = function(){ var tagNodes = document.querySelectorAll('div.desc span'); var tagStr = ''; tagNodes.forEach(tag =>{ tagStr += tag.innerText + '# '; }); console.log(tagStr);

    GM_setClipboard(tagStr);

}
//

function validateTitle(title) {
    var pattern = '/[\/:*?'<>~•|

]+?/g'; var newTitle = title.replace(/[/:?'<>•| ]/g, '_'); return newTitle.trim(); } // unsafeWindow.downloadImg = function(){ var title = validateTitle(document.querySelector('.title').textContent.trim()); var index = '_0' + document.querySelector('.fraction').textContent[0]; title += index; var imgUrl = document.querySelector('.swiper-slide-active[style='/format']').style.backgroundImage.match(/'(https?://[^']+)'/)[1].replace('webp', 'jpg'); console.log(imgUrl);

    var xhr = new XMLHttpRequest();
    xhr.open('GET', imgUrl, true);
    xhr.responseType = 'blob';
    xhr.onload = function () {
        if (this.status == 200) {
            var blob = this.response;
            var a = document.createElement('a');
            a.href = window.URL.createObjectURL(blob);
            a.download = title + '.jpg';
            a.style.display = 'none';
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
        }
    };
    xhr.send();
}

//
unsafeWindow.downloadMulti = function(){
    var title = validateTitle(document.querySelector('.title').textContent.trim());
    var imgNodes = document.querySelectorAll('.swiper-slide[data-swiper-slide-index]:not(.swiper-slide-duplicate)');

    imgNodes.forEach(function (aImg, i) {
        setTimeout(function () {
            var imgUrl = aImg.style.backgroundImage.match(/'(https?://[^']+)'/)[1].replace('webp', 'jpg');
            console.log(imgUrl);

            var xhr = new XMLHttpRequest();
            xhr.open('GET', imgUrl, true);
            xhr.responseType = 'blob';
            xhr.onload = function () {
                if (this.status == 200) {
                    var blob = this.response;
                    var a = document.createElement('a');
                    a.href = window.URL.createObjectURL(blob);
                    a.download = title + '_0' + (i + 1) + '.jpg';
                    a.style.display = 'none';
                    document.body.appendChild(a);
                    a.click();
                    document.body.removeChild(a);
                }
            };
            xhr.send();
        }, 200 * i);
    });
}
//
let myFloat = '<div id='myfloat'></div>';
let singleDownHtml = '<button id='download' title='会复制出文件名' class='myBtn' onclick = 'downloadImg();this.style.background='rgba(252,180,41,0.8)';'>下单图</button>';
let multipleDownHtml = '<button id='downloadMulti' title='会复制出文件名' class='myBtn' onclick = 'downloadMulti();this.style.background='rgba(252,180,41,0.8)';'>下多图</button>';
let copyTags = '<button id='copyout' title='会复制出所有 TAG' class='myBtn' onclick = 'copyOutTag();this.style.background='rgba(252,180,41,0.8)';'>拷Tag</button>';

// 使用 MutationObserver 监听 DOM 变化
const observer = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
        if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
            mutation.addedNodes.forEach(function(node) {
                if (node.id === 'myfloat') {
                    node.innerHTML += singleDownHtml + '<br />' + multipleDownHtml + '<br />' + copyTags;
                } else if (node.classList && node.classList.contains('fraction')) {
                    document.querySelector('#myfloat').style.top = '80px';
                }
            });
        }
    });
});
observer.observe(document.body, { childList: true, subtree: true });

//

// Your code here...

})();


原文地址: https://www.cveoy.top/t/topic/oYaz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录