豆瓣 电影详情链接新标签页打开

豆瓣电影 电影详情链接新标签页打开

// ==UserScript==
// @name         豆瓣 电影详情链接新标签页打开
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  豆瓣电影 电影详情链接新标签页打开
// @author       Brayden
// @license      AGPL-3.0-or-later
// @icon         data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAAAXNSR0IArs4c6QAAAUtJREFUSEvdll9ugkAQxudbPEBLewB9kKSnkJ5EfKQeQj2E8IicRD2FCTyUA7SkByg7zYbQQFzQjRHS7iMM+5tv/gLq4aAHBg0LeQ5Tt2BagXhspJYR58vpuv5NqxI7SNno8satvMt9Z1E90kKetonHQNQO4ax8hxaVnOVvzuQmiMT3RJC1Ioand2RoiB0mkSDELHncFa6blNhB8g6mjQpBzxDOCHSox15SsbGk5bKgWSMnTG5ZDB050SkB86IQRQPy5b9kD+HprLLAowikQIYQXfX81Zyca/k/SlQxSGBeJll3rkg8BDLJtL/LgKxK+HPp7FT3t8+mbrwAvX7409+yb0zhxyDdg+lY7QO1U9SIMVGkekr1Ues+sbfpmsBzYsSgapx3I1Ro617rrBtKVBcLOfIYNLt6I4IO9QV1EWISFhPbYX8kTDy9ZPsD3MfqGtJfu9YAAAAASUVORK5CYII=
// @match        https://*.douban.com/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    // 选择器以匹配您想要修改的a标签
    const selector = '.ui-slide-item a, .review a, .item-root a, .recommendations-bd a, .review-list a, .grid_view ul a';

    // 打开新标签页的函数
    function setOpenInNewTab() {
        const externalLinks = document.querySelectorAll(selector);
        externalLinks.forEach(function (link) {
            if (link.target !== '_blank' && !link.getAttribute('href').startsWith('javascript:')) {
                link.target = '_blank';
            }
        });
    }

    // 监听DOM变化的函数
    function observePageChanges() {
        var observer = new MutationObserver(mutations => {
            setOpenInNewTab();
        });
        var config = { childList: true, subtree: true };
        observer.observe(document.body, config);
    }

    // 初始化
    setOpenInNewTab();
    observePageChanges();
})();