* 自适应全局主题颜色修改器 * @author hmjz100 * @description 自动遍历并替换 `页面所有样式表` `SVG 元素` 的颜色值 * @param {Array<[String, String]>} colorMap - 颜色映射表 * @param {"default"|"other"} type - 替换模式
(colorMap, type)
| 1749 | * @param {"default"|"other"} type - 替换模式 |
| 1750 | */ |
| 1751 | adaptiveThemeOverride(colorMap, type) { |
| 1752 | base.waitForKeyElements(`[${mount}^="${mount}-ColorUI-"], [id^="${mount}-ColorUI-"]`, function (tag) { |
| 1753 | if (tag.html() === base.adaptiveStyleOverride(tag.text(), "", type, colorMap)) return; |
| 1754 | let cssText = base.adaptiveStyleOverride(tag.text(), "", type, colorMap); |
| 1755 | base.addStyle(tag.attr(mount), "style", cssText, tag[0]); |
| 1756 | return true; |
| 1757 | }, true) |
| 1758 | base.waitForKeyElements(`[data-pl-colored]`, function (tag) { |
| 1759 | if (tag.attr("data-pl-colored") === temp.color) return; |
| 1760 | let originalStyle = tag.attr("style"); |
| 1761 | if (!originalStyle) return; |
| 1762 | let newStyle = base.adaptiveStyleOverride(originalStyle, "", type, colorMap); |
| 1763 | if (newStyle !== originalStyle) { |
| 1764 | tag.attr("style", newStyle); |
| 1765 | } |
| 1766 | return true; |
| 1767 | }, true); |
| 1768 | let count = 0; |
| 1769 | if (!temp.colored) { |
| 1770 | base.waitForKeyElements(`link[rel="stylesheet"]`, function (tag) { |
| 1771 | if (!tag.parent().length || !tag.attr("href")) return; |
| 1772 | let href = tag.attr("href"); |
| 1773 | try { |
| 1774 | href = new URL(href, location.href).href; |
| 1775 | } catch (e) { |
| 1776 | return; |
| 1777 | } |
| 1778 | fetch(href) |
| 1779 | .then(response => response.text()) |
| 1780 | .then(responseText => { |
| 1781 | let id = `${mount}-ColorUI-` + href.replace(/[^\w]/g, "_"); |
| 1782 | let cssText = base.adaptiveStyleOverride(responseText, href, type, colorMap); |
| 1783 | if (responseText === base.adaptiveStyleOverride(responseText, href, type, colorMap)) return; |
| 1784 | base.addStyle(id, "style", cssText, tag[0], "after"); |
| 1785 | }) |
| 1786 | }, true); |
| 1787 | base.waitForKeyElements(`style:not([${mount}^="${mount}-"],[id^="swal-pub"],[class^="darkreader"])`, function (tag) { |
| 1788 | let id = tag.attr(mount); |
| 1789 | let text = tag.html() |
| 1790 | if (tag.data("styles") === text) return; |
| 1791 | tag.data("styles", text); |
| 1792 | // 替换颜色并添加样式 |
| 1793 | let cssText = base.adaptiveStyleOverride(text, "", type, colorMap); |
| 1794 | if (text === cssText) return; |
| 1795 | id = id ? id : `${mount}-ColorUI-${count++}` |
| 1796 | base.addStyle(id, "style", cssText, tag[0], "after"); |
| 1797 | }, true) |
| 1798 | base.waitForKeyElements("svg", function (element) { |
| 1799 | element.find("*").each((index, element) => { |
| 1800 | let fill = $(element).attr("fill"); |
| 1801 | let stroke = $(element).attr("stroke"); |
| 1802 | if (fill) { |
| 1803 | let newFill = base.adaptiveStyleOverride(fill, "", type, colorMap); |
| 1804 | if (newFill !== fill) { |
| 1805 | $(element).attr("fill", newFill); |
| 1806 | } |
| 1807 | } |
| 1808 | if (stroke) { |