@@ -8,7 +8,7 @@ chrome.storage.onChanged.addListener((changes, areaName) => {
88 if ( changes . chooseTheme . newValue ) {
99 const new_flomo_theme = {
1010 id : changes . chooseTheme . newValue ,
11- matches : [ 'https://v.flomoapp.com/*' , 'https://flomoapp.com/*' , 'https://h5.udrig.com/*' ] ,
11+ matches : [ 'https://v.flomoapp.com/*' , 'https://flomoapp.com/*' ] ,
1212 css : [ 'include/theme/' + changes . chooseTheme . newValue ] ,
1313 runAt : 'document_end' ,
1414 } ;
@@ -51,7 +51,7 @@ function getTheme() {
5151
5252const flomo_content_script = {
5353 id : 'flomo_content_script' ,
54- matches : [ 'https://v.flomoapp.com/*' , 'https://flomoapp.com/*' , 'https://h5.udrig.com/*' ] ,
54+ matches : [ 'https://v.flomoapp.com/*' , 'https://flomoapp.com/*' ] ,
5555 js : [ 'scripts/content.js' , 'include/highlight.min.js' ] ,
5656 runAt : 'document_end' ,
5757} ;
@@ -72,129 +72,9 @@ chrome.webRequest.onCompleted.addListener(
7272 }
7373
7474 // 在请求所在的tab中执行代码高亮功能
75- chrome . tabs . sendMessage ( details . tabId , { action : 'highlightCode' } , function ( response ) {
76- if ( chrome . runtime . lastError ) {
77- console . error ( '发送消息错误:' , chrome . runtime . lastError . message ) ;
78-
79- // 如果发送消息失败,可能是content script还没有加载,尝试执行代码
80- chrome . scripting . executeScript ( {
81- target : { tabId : details . tabId } ,
82- function : triggerHighlight ,
83- } ) ;
84- } else {
85- console . log ( '已触发代码高亮' ) ;
86- }
87- } ) ;
75+ chrome . tabs . sendMessage ( details . tabId , { action : 'highlightCode' } ) ;
8876 } ) ;
8977 }
9078 } ,
9179 { urls : [ 'https://h5.udrig.com/app/v1*' ] }
9280) ;
93-
94- // 用于执行的函数
95- function triggerHighlight ( ) {
96- // 这个函数会在目标页面中执行
97- console . log ( '正在执行代码高亮' ) ;
98- if ( window . onload ) {
99- // 手动触发window.onload中定义的高亮函数
100- const memos = document . getElementsByClassName ( 'richText' ) ;
101- const memos_array = Array . from ( memos ) ;
102- memos_array . forEach ( ( memo ) => {
103- let children = memo . children ;
104- const memo_p_array = Array . from ( children ) ;
105- let languageFlag = false ;
106- let codeContentArr = [ ] ;
107- let languageType = '' ;
108- for ( let i = 0 ; i < memo_p_array . length ; i ++ ) {
109- if ( memo_p_array [ i ] . innerHTML . startsWith ( '```' ) && memo_p_array [ i ] . innerHTML . substring ( 3 ) != '' ) {
110- languageType = 'language-' + memo_p_array [ i ] . innerHTML . substring ( 3 ) ;
111- languageFlag = true ;
112- memo . removeChild ( memo_p_array [ i ] ) ;
113- continue ;
114- } else if (
115- memo_p_array [ i ] . innerHTML . startsWith ( '```' ) &&
116- memo_p_array [ i ] . innerHTML . substring ( 3 ) === ''
117- ) {
118- languageFlag = false ;
119-
120- const codeForCopy = [ ...codeContentArr ] ;
121-
122- let pre = document . createElement ( 'pre' ) ;
123- let code = document . createElement ( 'code' ) ;
124- code . innerHTML = codeContentArr . join ( '\n' ) ;
125- code . className = languageType ;
126-
127- let wrapper = document . createElement ( 'div' ) ;
128- wrapper . style . position = 'relative' ;
129-
130- // 创建复制按钮
131- let copyBtn = document . createElement ( 'button' ) ;
132- copyBtn . innerHTML =
133- '<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>' ;
134- copyBtn . style . position = 'absolute' ;
135- copyBtn . style . top = '5px' ;
136- copyBtn . style . right = '5px' ;
137- copyBtn . style . zIndex = '100' ;
138- copyBtn . style . fontSize = '12px' ;
139- copyBtn . style . padding = '4px' ;
140- copyBtn . style . background = 'rgba(240, 240, 240, 0.8)' ;
141- copyBtn . style . border = '1px solid #ccc' ;
142- copyBtn . style . borderRadius = '3px' ;
143- copyBtn . style . cursor = 'pointer' ;
144- copyBtn . style . display = 'flex' ;
145- copyBtn . style . justifyContent = 'center' ;
146- copyBtn . style . alignItems = 'center' ;
147- copyBtn . title = '复制代码' ;
148-
149- copyBtn . addEventListener ( 'click' , function ( e ) {
150- e . stopPropagation ( ) ;
151-
152- const tempElement = document . createElement ( 'div' ) ;
153-
154- let decodedContent = [ ] ;
155- for ( let i = 0 ; i < codeForCopy . length ; i ++ ) {
156- tempElement . innerHTML = codeForCopy [ i ] ;
157- decodedContent . push ( tempElement . textContent || tempElement . innerText ) ;
158- }
159-
160- const codeText = decodedContent . join ( '\n' ) ;
161-
162- navigator . clipboard
163- . writeText ( codeText )
164- . then ( ( ) => {
165- copyBtn . style . color = '#4CAF50' ;
166- copyBtn . title = '已复制!' ;
167-
168- setTimeout ( ( ) => {
169- copyBtn . style . color = '' ;
170- copyBtn . title = '复制代码' ;
171- } , 1500 ) ;
172- } )
173- . catch ( ( err ) => {
174- console . error ( '复制失败:' , err ) ;
175- } ) ;
176- } ) ;
177-
178- pre . appendChild ( code ) ;
179- wrapper . appendChild ( pre ) ;
180- wrapper . appendChild ( copyBtn ) ;
181-
182- memo_p_array [ i ] . innerHTML = '' ;
183- memo_p_array [ i ] . appendChild ( wrapper ) ;
184-
185- codeContentArr = [ ] ;
186- languageType = '' ;
187- continue ;
188- } else {
189- if ( languageFlag ) {
190- codeContentArr . push ( memo_p_array [ i ] . innerHTML ) ;
191- memo . removeChild ( memo_p_array [ i ] ) ;
192- }
193- }
194- }
195- } ) ;
196- if ( typeof hljs !== 'undefined' ) {
197- hljs . highlightAll ( ) ;
198- }
199- }
200- }
0 commit comments