2
2
3
3
// declare debug mode var
4
4
let gitdDebugMode = false
5
+ let gitdProcessRunning = false
6
+
7
+ // templates
8
+ const gitdInitTemplateContainer = `<div id="gitd-container-template"><div class="gitd-shortcut-button">
9
+ <button id="gitdStartButton" @click="activateGitdInit" type="button" class="gitd-btn gitd-btn-sm gitd-btn-warning">
10
+ <svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" fill="currentColor" class="bi bi-card-checklist" viewBox="0 0 16 16">
11
+ <path d="M14.5 3a.5.5 0 0 1 .5.5v9a.5.5 0 0 1-.5.5h-13a.5.5 0 0 1-.5-.5v-9a.5.5 0 0 1 .5-.5h13zm-13-1A1.5 1.5 0 0 0 0 3.5v9A1.5 1.5 0 0 0 1.5 14h13a1.5 1.5 0 0 0 1.5-1.5v-9A1.5 1.5 0 0 0 14.5 2h-13z"/>
12
+ <path d="M7 5.5a.5.5 0 0 1 .5-.5h5a.5.5 0 0 1 0 1h-5a.5.5 0 0 1-.5-.5zm-1.496-.854a.5.5 0 0 1 0 .708l-1.5 1.5a.5.5 0 0 1-.708 0l-.5-.5a.5.5 0 1 1 .708-.708l.146.147 1.146-1.147a.5.5 0 0 1 .708 0zM7 9.5a.5.5 0 0 1 .5-.5h5a.5.5 0 0 1 0 1h-5a.5.5 0 0 1-.5-.5zm-1.496-.854a.5.5 0 0 1 0 .708l-1.5 1.5a.5.5 0 0 1-.708 0l-.5-.5a.5.5 0 0 1 .708-.708l.146.147 1.146-1.147a.5.5 0 0 1 .708 0z"/>
13
+ </svg>
14
+ <span x-text="gitdInitText">Gitd Start</span>
15
+ </button>
16
+ </div></div>`
17
+
18
+ // track body change (x-data)
19
+ // trackBodyAttributes("x-data")
5
20
6
21
// listen runtime message from bg
7
22
chrome . runtime . onMessage . addListener ( function ( request , sender , sendResponse ) {
8
23
switch ( request . action ) {
9
- case "IM_CHANGED" :
24
+ /* case "IM_CHANGED":
10
25
// Dom Content Loading but not finish
11
- if ( isDebugActive ( ) ) console . log ( "content-script" , "im changed" )
26
+ if (gitdDebugMode ) console.log("content-script", "im changed")
12
27
13
- // inject templates
14
- injectGitdTemplates ( )
28
+ // start process
29
+ // initGitdProcess("IM_CHANGED" )
15
30
16
- setTimeout ( function ( ) {
17
- // Trigger the button element with a click
18
- triggerGitdStart ( )
19
- } , 1500 )
20
-
21
- break ;
31
+ break;*/
22
32
/*case "IM_LOADING":
23
33
// Dom Content Loading but not finish
24
- if (isDebugActive() ) console.log("content-script", "im loading")
34
+ if (gitdDebugMode ) console.log("content-script", "im loading")
25
35
26
36
break;*/
37
+ case "URL_CHANGED" :
38
+ // url changed
39
+ if ( gitdDebugMode ) console . log ( "content-script" , "url changed" )
40
+
41
+ // start process
42
+ initGitdProcess ( "URL_CHANGED" )
43
+
44
+ break ;
27
45
case "IM_READY" :
28
46
// Everything Loading finished. Ready to use.
29
- if ( isDebugActive ( ) ) console . log ( "content-script" , "im ready" )
30
-
31
- // inject templates
32
- injectGitdTemplates ( )
47
+ if ( gitdDebugMode ) console . log ( "content-script" , "im ready" )
33
48
34
- setTimeout ( function ( ) {
35
- // Trigger the button element with a click
36
- triggerGitdStart ( )
37
- } , 1500 )
49
+ // start process
50
+ initGitdProcess ( "IM_READY" )
38
51
39
52
// inject gitdmanager
40
53
injectGitdScripts ( "lib/gitdmanager.js" )
@@ -52,12 +65,13 @@ chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
52
65
} ) ;
53
66
54
67
// listen browser submit event
68
+ // via: chrome dev tools: getEventListeners(window) -> return all events
55
69
// Request: gitdmanager (browser) -> contentScript -> background
56
70
// Response: background -> contentScript -> gitdmanager (browser)
57
71
window . addEventListener ( "submit-action" , function ( evt ) {
58
- if ( isDebugActive ( ) ) console . log ( "content-script" , "submit-action" , evt . detail )
72
+ if ( gitdDebugMode ) console . log ( "content-script" , "submit-action" , evt . detail )
59
73
chrome . runtime . sendMessage ( JSON . parse ( evt . detail ) , function ( response ) {
60
- if ( isDebugActive ( ) ) console . log ( "bg-response" , response ) ;
74
+ if ( gitdDebugMode ) console . log ( "bg-response" , response ) ;
61
75
62
76
window . dispatchEvent ( new CustomEvent (
63
77
'submit-action-response' ,
@@ -72,37 +86,75 @@ window.addEventListener("submit-action", function(evt) {
72
86
// via: chrome dev tools: getEventListeners(window) -> return all events
73
87
// only for github "turbo:load" event listen
74
88
// window.addEventListener("turbo:load", function(evt) {
75
- // if (isDebugActive()) console.log("content-script", "turbo:load", evt)
76
-
77
- // setTimeout(function() {
78
- // // inject templates
79
- // injectGitdTemplates()
89
+ // if (gitdDebugMode) console.log("content-script", "turbo:load", evt)
80
90
81
- // // init checkboxes
82
- // initQuickSelectionCheckboxes()
83
- // }, 1500)
91
+ // // start process
92
+ // initGitdProcess("turbo:load")
84
93
85
94
// }, false);
86
95
87
96
// debug mode listener
88
97
window . addEventListener ( "debug-mode-changed" , function ( evt ) {
89
- if ( isDebugActive ( ) ) console . log ( "content-script" , "debug-mode-changed" , evt )
98
+ if ( gitdDebugMode ) console . log ( "content-script" , "debug-mode-changed" , evt )
90
99
91
100
gitdDebugMode = evt . detail
92
101
93
102
} , false ) ;
94
103
95
- // check debug mode
96
- function isDebugActive ( ) {
97
- return gitdDebugMode
98
- }
104
+ // init gitd with process control
105
+ function initGitdProcess ( where ) {
106
+ if ( gitdProcessRunning ) {
107
+ if ( gitdDebugMode ) console . log ( "gitdProcessRunning is not finished yet" , where ) ;
108
+ return
109
+ }
110
+
111
+ // process start
112
+ gitdProcessRunning = true
113
+
114
+ if ( document . getElementById ( "gitdStartButton" ) === null ) {
115
+ /////////////////// inject templates
116
+ // remove template
117
+ if ( document . getElementById ( "gitd-container-template" ) ) {
118
+ document . getElementById ( "gitd-container-template" ) . remove ( )
119
+ }
99
120
100
- // inject templates
101
- function injectGitdTemplates ( ) {
102
- if ( ! document . body . hasAttribute ( "x-data" ) ) {
121
+ // remove checkboxes if exists
122
+ let gitdCheckboxes = document . querySelectorAll ( ".gitd-tree-checkbox-container" )
123
+ if ( gitdCheckboxes . length > 0 ) {
124
+ // remove checkbox
125
+ gitdCheckboxes . forEach ( e => e . remove ( ) )
126
+ }
127
+
128
+ // add x-data and template container
103
129
document . body . setAttribute ( "x-data" , "gitdManager" )
104
- document . body . insertAdjacentHTML ( "beforeend" , gitdInitTemplate )
130
+ document . body . insertAdjacentHTML ( "beforeend" , gitdInitTemplateContainer )
105
131
}
132
+
133
+ //////////////////// triggger button
134
+ // if (where === "IM_CHANGED") {
135
+ // hostname
136
+ let hostname = window . location . hostname
137
+
138
+ if ( hostname === "github.com" ) {
139
+ setTimeout ( function ( ) {
140
+ if ( gitdDebugMode ) console . log ( "triggerGitdStart" ) ;
141
+
142
+ let gitdStartButton = document . getElementById ( "gitdStartButton" )
143
+ if ( ! ! gitdStartButton && ( gitdStartButton . getAttribute ( "data-trigger" ) === null || where === "URL_CHANGED" ) ) {
144
+ if ( gitdDebugMode ) console . log ( "gitdStartButton" , "trigger" , "click" , gitdStartButton )
145
+
146
+ gitdStartButton . setAttribute ( "data-trigger" , where )
147
+ gitdStartButton . click ( )
148
+ }
149
+
150
+ // process finished
151
+ gitdProcessRunning = false
152
+ } , 1500 )
153
+ }
154
+ // }
155
+
156
+ // // process finished
157
+ // gitdProcessRunning = false
106
158
}
107
159
108
160
// inject scripts
@@ -113,71 +165,5 @@ function injectGitdScripts(scrPath) {
113
165
s . parentNode . removeChild ( s ) ;
114
166
} ;
115
167
( document . body || document . documentElement ) . appendChild ( s )
116
- if ( isDebugActive ( ) ) console . log ( "injectGitdScripts" , s ) ;
117
- }
118
-
119
- // trigger gitdStartButton
120
- function triggerGitdStart ( ) {
121
- let gitdStartButton = document . getElementById ( "gitdStartButton" )
122
- if ( ! ! gitdStartButton ) {
123
- if ( isDebugActive ( ) ) console . log ( "gitdStartButton" , "trigger" , "click" , gitdStartButton )
124
- gitdStartButton . click ( )
125
- }
168
+ if ( gitdDebugMode ) console . log ( "injectGitdScripts" , s ) ;
126
169
}
127
-
128
- // init quick selection checkboxes
129
- // function initQuickSelectionCheckboxes() {
130
- // // check gitd-init attribute
131
- // if (document.querySelector(".gitd-tree-checkbox") === null) {
132
-
133
- // // check new design
134
- // let newDesign = false
135
-
136
- // // checkbox add
137
- // let navItem = document.querySelectorAll("div.js-navigation-item > div:first-child > svg") // old design
138
- // if (navItem.length == 0) {
139
- // navItem = document.querySelectorAll("table > tbody > tr > td > div.react-directory-filename-column > svg") // new design
140
- // newDesign = true
141
- // }
142
-
143
- // if (navItem.length > 0) {
144
-
145
- // // inject checkbox
146
- // for (const key in navItem) {
147
- // if (Object.hasOwnProperty.call(navItem, key)) {
148
- // const element = navItem[key];
149
-
150
- // // type: none: 0 - file: 1 - folder: 2
151
- // let itemType = element.getAttribute("aria-label") // old desing
152
- // if (newDesign) {
153
- // itemType = element.getAttribute("class") // new design
154
- // }
155
-
156
- // if (itemType === "Directory") {
157
- // itemType = 2
158
- // } else if (itemType === "File") {
159
- // itemType = 1
160
- // } else if (itemType == "icon-directory") {
161
- // itemType = 2
162
- // } else {
163
- // itemType = 1
164
- // }
165
-
166
- // let pathElement = element.parentElement.nextElementSibling?.querySelector("div > span > a") // old desing
167
- // if (newDesign) {
168
- // pathElement = element.parentElement.querySelector("div.overflow-hidden > h3 > div > a") // new design
169
- // }
170
-
171
- // if (!!pathElement) {
172
- // let insertPosition = "beforebegin"
173
- // if (newDesign) {
174
- // insertPosition = "afterbegin"
175
- // }
176
-
177
- // element.parentElement.insertAdjacentHTML(insertPosition, "<div role=\"gridcell\" class=\"mr-3 flex-shrink-0\"><input class=\"gitd-tree-checkbox\" type=\"checkbox\" data-name=\""+pathElement.innerText+"\" data-type=\""+itemType+"\" @click=\"toggleSelectList\"></div>")
178
- // }
179
- // }
180
- // }
181
- // }
182
- // }
183
- // }
0 commit comments