Skip to content

Commit

Permalink
优化动态设置icon
Browse files Browse the repository at this point in the history
  • Loading branch information
027xiguapi committed Jul 30, 2024
1 parent 804bffa commit 8b0e07d
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 20 deletions.
17 changes: 3 additions & 14 deletions contents/51cto.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import type { PlasmoCSConfig } from "plasmo"
import { useEffect } from "react"

import { useStorage } from "@plasmohq/storage/hook"
import { sendToBackground } from "@plasmohq/messaging"

import { addCss } from "~tools"
import { addCss, setIcon } from "~tools"

export const config: PlasmoCSConfig = {
matches: ["https://*.51cto.com/*"]
Expand All @@ -25,20 +24,10 @@ export default function Cto51() {
copyCode && copyCodeFunc()
}, 500)
closeLoginModal && closeLoginModalFunc()
if (copyCode || closeLoginModal) {
setIconFunc()
}
console.log(55, (copyCode || closeLoginModal))
setIcon((copyCode || closeLoginModal))
}, [copyCode, closeLoginModal])

function setIconFunc() {
sendToBackground({
name: "icon",
body: {
active: true
}
})
}

/* 未登录复制代码 */
function copyCodeCssFunc() {
addCss(`
Expand Down
2 changes: 2 additions & 0 deletions contents/cnblogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { PlasmoCSConfig } from "plasmo"
import { useEffect, useRef } from "react"

import { useStorage } from "@plasmohq/storage/hook"
import { setIcon } from "~tools"

export const config: PlasmoCSConfig = {
matches: ["https://*.cnblogs.com/*"],
Expand All @@ -18,6 +19,7 @@ export default function cnblogs() {
useEffect(() => {
console.log("cnblogs copyCode", copyCode)
copyCode && copyCodeFunc()
setIcon((copyCode))
}, [copyCode])

// 功能一: 修改复制按钮,支持一键复制
Expand Down
3 changes: 2 additions & 1 deletion contents/csdn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useEffect, useRef } from "react"

import { useStorage } from "@plasmohq/storage/hook"

import { addCss } from "~tools"
import { addCss, setIcon } from "~tools"

export const config: PlasmoCSConfig = {
matches: ["https://*.blog.csdn.net/*"]
Expand Down Expand Up @@ -36,6 +36,7 @@ const csdn = () => {
closeFollow && followFunc()
closeLoginModal && closeLoginModalFunc()
closeRedirectModal && closeRedirectModalFunc()
setIcon((closeAds || copyCode || autoOpenCode || closeFollow || closeLoginModal || closeRedirectModal))
}, [
closeAds,
copyCode,
Expand Down
3 changes: 2 additions & 1 deletion contents/jb51.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useEffect } from "react"

import { useStorage } from "@plasmohq/storage/hook"

import { addCss } from "~tools"
import { addCss, setIcon } from "~tools"

export const config: PlasmoCSConfig = {
matches: ["https://*.jb51.net/*"]
Expand All @@ -21,6 +21,7 @@ export default function jb51() {
console.log("jb51 status", { closeAds, copyCode })
closeAds && closeAdsFunc()
copyCode && copyCodeFunc()
setIcon((closeAds || copyCode))
}, [closeAds, copyCode])

/* 未登录复制代码 */
Expand Down
3 changes: 2 additions & 1 deletion contents/jianshu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useEffect, useRef } from "react"

import { useStorage } from "@plasmohq/storage/hook"

import { addCss } from "~tools"
import { addCss, setIcon } from "~tools"

export const config: PlasmoCSConfig = {
matches: ["https://*.jianshu.com/*"]
Expand All @@ -23,6 +23,7 @@ export default function Jianshu() {
copyCode && copyCodeFunc()
closeLoginModal && closeLoginModalFunc()
autoOpenCode && autoOpenCodeFunc()
setIcon((closeLoginModal || copyCode || autoOpenCode))
}, [copyCode, closeLoginModal, autoOpenCode])

// 一键复制
Expand Down
3 changes: 2 additions & 1 deletion contents/php.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useEffect } from "react"

import { useStorage } from "@plasmohq/storage/hook"

import { addCss } from "~tools"
import { addCss, setIcon } from "~tools"

export const config: PlasmoCSConfig = {
matches: ["https://*.php.cn/*"]
Expand All @@ -24,6 +24,7 @@ export default function Php() {
copyCode && copyCodeFunc()
}, 500)
closeLoginModal && closeLoginModalFunc()
setIcon((closeLoginModal || copyCode))
}, [copyCode, closeLoginModal])

/* 未登录复制代码 */
Expand Down
3 changes: 2 additions & 1 deletion contents/zhihu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useEffect, useRef } from "react"

import { useStorage } from "@plasmohq/storage/hook"

import { addCss } from "~tools"
import { addCss, setIcon } from "~tools"

export const config: PlasmoCSConfig = {
matches: ["https://*.zhihu.com/*"]
Expand All @@ -23,6 +23,7 @@ export default function zhihu() {
copyCode && copyCodeFunc()
closeLoginModal && closeLoginModalFunc()
autoOpenCode && autoOpenCodeFunc()
setIcon((closeLoginModal || copyCode || autoOpenCode))
}, [copyCode, closeLoginModal, autoOpenCode])

// 功能一: 修改复制按钮,支持一键复制
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "code-box",
"displayName": "codebox-免登录一键复制代码(CSDN/知乎/脚本之家/博客园/51CTO博客)",
"version": "0.2.6",
"version": "0.2.7",
"description": "本浏览器插件可以用于CSDN/知乎/脚本之家/博客园/51CTO博客/php中文等网站,实现无需登录一键复制代码;支持选中代码;或者代码右上角按钮的一键复制;解除关注博主即可阅读全文提示;去除登录弹窗;去除跳转APP弹窗;页面自定义样式.",
"author": "027xiguapi. <458813868@qq.com>",
"scripts": {
Expand Down
11 changes: 11 additions & 0 deletions tools.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { sendToBackground } from "@plasmohq/messaging"

export function addCss(code) {
const style = document.createElement("style")
const css = document.createTextNode(code)
Expand All @@ -12,3 +14,12 @@ export function addJs(code) {
script.appendChild(js)
document.head.appendChild(script)
}

export function setIcon(active: boolean) {
sendToBackground({
name: "icon",
body: {
active: active
}
})
}

0 comments on commit 8b0e07d

Please sign in to comment.