11// ==UserScript==
22// @name GitHub Commit Labels
33// @namespace https://github.com/nazdridoy
4- // @version 1.4 .0
4+ // @version 1.5 .0
55// @description Enhances GitHub commits with beautiful labels for conventional commit types (feat, fix, docs, etc.)
66// @author nazdridoy
77// @license MIT
@@ -158,6 +158,7 @@ SOFTWARE.
158158 enableTooltips : true ,
159159 labelsVisible : true ,
160160 showScope : false ,
161+ debugMode : false , // Add debug mode setting
161162 labelStyle : {
162163 fontSize : '14px' ,
163164 fontWeight : '500' ,
@@ -619,6 +620,21 @@ SOFTWARE.
619620 scopeDiv . appendChild ( scopeLabel ) ;
620621 configWindow . insertBefore ( scopeDiv , floatingBtnDiv . nextSibling ) ;
621622
623+ // Add debug mode toggle
624+ const debugDiv = document . createElement ( 'div' ) ;
625+ debugDiv . style . marginBottom = '20px' ;
626+ const debugCheckbox = document . createElement ( 'input' ) ;
627+ debugCheckbox . type = 'checkbox' ;
628+ debugCheckbox . checked = USER_CONFIG . debugMode ;
629+ debugCheckbox . id = 'debug-mode' ;
630+ debugCheckbox . style . marginRight = '5px' ;
631+ const debugLabel = document . createElement ( 'label' ) ;
632+ debugLabel . htmlFor = 'debug-mode' ;
633+ debugLabel . textContent = 'Enable debug mode (shows detailed logs in console)' ;
634+ debugDiv . appendChild ( debugCheckbox ) ;
635+ debugDiv . appendChild ( debugLabel ) ;
636+ configWindow . insertBefore ( debugDiv , prefixDiv . nextSibling ) ;
637+
622638 // Commit Types Configuration
623639 const typesContainer = document . createElement ( 'div' ) ;
624640 typesContainer . style . marginBottom = '20px' ;
@@ -955,6 +971,7 @@ SOFTWARE.
955971 newConfig . enableTooltips = tooltipCheckbox . checked ;
956972 newConfig . showFloatingButton = floatingBtnCheckbox . checked ;
957973 newConfig . showScope = scopeCheckbox . checked ;
974+ newConfig . debugMode = debugCheckbox . checked ; // Add debug mode
958975 newConfig . commitTypes = { } ;
959976
960977 typesContainer . querySelectorAll ( 'input, select' ) . forEach ( input => {
@@ -1200,12 +1217,26 @@ SOFTWARE.
12001217 }
12011218 }
12021219
1220+ // Debug logging function
1221+ function debugLog ( message , data = null ) {
1222+ if ( USER_CONFIG . debugMode ) {
1223+ const timestamp = new Date ( ) . toISOString ( ) ;
1224+ const logMessage = `[GitHub Commit Labels Debug] [${ timestamp } ] ${ message } ` ;
1225+ console . log ( logMessage ) ;
1226+ if ( data ) {
1227+ console . log ( 'Data:' , data ) ;
1228+ }
1229+ }
1230+ }
1231+
12031232 // Helper function to safely query elements
12041233 function safeQuerySelector ( selector ) {
12051234 try {
1206- return document . querySelectorAll ( selector ) ;
1235+ const elements = document . querySelectorAll ( selector ) ;
1236+ debugLog ( `Query selector "${ selector } " found ${ elements . length } elements` ) ;
1237+ return elements ;
12071238 } catch ( error ) {
1208- console . warn ( '[GitHub Commit Labels] Selector error:' , error ) ;
1239+ debugLog ( ` Selector error for " ${ selector } ":` , error ) ;
12091240 return [ ] ;
12101241 }
12111242 }
@@ -1225,14 +1256,20 @@ SOFTWARE.
12251256
12261257 // Main function to add labels to commits
12271258 function addCommitLabels ( ) {
1259+ debugLog ( 'Starting addCommitLabels' ) ;
1260+
12281261 // Only proceed if we're on a commit page
1229- if ( ! isCommitPage ( ) ) return ;
1262+ if ( ! isCommitPage ( ) ) {
1263+ debugLog ( 'Not on a commit page, exiting' ) ;
1264+ return ;
1265+ }
12301266
1231- // Update theme colors
1267+ debugLog ( 'Updating theme colors' ) ;
12321268 updateThemeColors ( ) ;
12331269
12341270 // Create toggle button if it doesn't exist and is enabled
12351271 if ( USER_CONFIG . showFloatingButton !== false ) {
1272+ debugLog ( 'Creating label toggle button' ) ;
12361273 createLabelToggle ( ) ;
12371274 }
12381275
@@ -1243,37 +1280,53 @@ SOFTWARE.
12431280 '.markdown-title a[data-pjax="true"]' // Legacy
12441281 ] ;
12451282
1283+ debugLog ( 'Trying selectors:' , selectors ) ;
12461284 let commitMessages = [ ] ;
12471285 for ( const selector of selectors ) {
12481286 commitMessages = safeQuerySelector ( selector ) ;
12491287 if ( commitMessages . length > 0 ) {
1250- console . log ( '[GitHub Commit Labels] Using selector:' , selector ) ;
1288+ debugLog ( ` Using selector: ${ selector } ` ) ;
12511289 break ;
12521290 }
12531291 }
12541292
1293+ debugLog ( `Found ${ commitMessages . length } commit messages to process` ) ;
1294+
12551295 // Debounce and batch process for performance improvement
12561296 let processedCount = 0 ;
12571297 const batchSize = 20 ;
12581298 const commitMessagesArray = Array . from ( commitMessages ) ;
12591299
12601300 const processCommitBatch = ( startIndex ) => {
1301+ debugLog ( `Processing batch starting at index ${ startIndex } ` ) ;
12611302 const endIndex = Math . min ( startIndex + batchSize , commitMessagesArray . length ) ;
12621303
12631304 for ( let i = startIndex ; i < endIndex ; i ++ ) {
12641305 try {
12651306 const message = commitMessagesArray [ i ] ;
12661307 const text = message . textContent . trim ( ) ;
1308+ debugLog ( `Processing commit message: ${ text } ` ) ;
1309+
1310+ // Skip if this commit already has a label
1311+ if ( message . parentElement . querySelector ( '.commit-label' ) ) {
1312+ debugLog ( 'Commit already has a label, skipping' ) ;
1313+ continue ;
1314+ }
1315+
12671316 const match = text . match ( / ^ ( \w + ) (?: \( ( [ \w - ] + ) \) ) ? : \s * ( .* ) / ) ;
1317+ debugLog ( 'Commit message match result:' , match ) ;
12681318
12691319 if ( match ) {
12701320 const type = match [ 1 ] . toLowerCase ( ) ;
12711321 const scope = match [ 2 ] || '' ;
12721322 const restOfMessage = match [ 3 ] ;
1323+ debugLog ( `Extracted: type=${ type } , scope=${ scope } , message=${ restOfMessage } ` ) ;
12731324
12741325 if ( USER_CONFIG . commitTypes [ type ] ) {
1326+ debugLog ( `Found matching commit type: ${ type } ` ) ;
12751327 // Only add label if it hasn't been added yet
12761328 if ( ! message . parentElement . querySelector ( '.commit-label' ) ) {
1329+ debugLog ( 'Creating new label' ) ;
12771330 const label = document . createElement ( 'span' ) ;
12781331 label . className = 'commit-label' ;
12791332 label . dataset . commitType = type ;
@@ -1411,32 +1464,54 @@ SOFTWARE.
14111464 scopeSpan . style . fontSize = '12px' ;
14121465 labelText . appendChild ( scopeSpan ) ;
14131466 }
1467+ } else {
1468+ debugLog ( 'Label already exists, skipping' ) ;
14141469 }
1470+ } else {
1471+ debugLog ( `No matching commit type found for: ${ type } ` ) ;
1472+ }
1473+ } else {
1474+ // Only log non-conventional commits if they don't have a label
1475+ if ( ! message . parentElement . querySelector ( '.commit-label' ) ) {
1476+ debugLog ( 'Commit message does not match conventional commit format and has no label' ) ;
1477+ } else {
1478+ debugLog ( 'Skipping already processed commit' ) ;
14151479 }
14161480 }
14171481 } catch ( error ) {
1418- console . warn ( '[GitHub Commit Labels] Error processing commit:', error ) ;
1482+ debugLog ( ' Error processing commit:', error ) ;
14191483 }
14201484 }
14211485
14221486 // Process next batch if needed
14231487 processedCount += ( endIndex - startIndex ) ;
1488+ debugLog ( `Processed ${ processedCount } of ${ commitMessagesArray . length } commits` ) ;
1489+
14241490 if ( processedCount < commitMessagesArray . length ) {
1491+ debugLog ( 'Scheduling next batch' ) ;
14251492 setTimeout ( ( ) => processCommitBatch ( endIndex ) , 0 ) ;
1493+ } else {
1494+ debugLog ( 'Finished processing all commits' ) ;
14261495 }
14271496 } ;
14281497
14291498 // Start processing first batch
14301499 if ( commitMessagesArray . length > 0 ) {
1500+ debugLog ( 'Starting first batch processing' ) ;
14311501 processCommitBatch ( 0 ) ;
1502+ } else {
1503+ debugLog ( 'No commit messages found to process' ) ;
14321504 }
14331505 }
14341506
14351507 // Set up MutationObserver to watch for DOM changes
14361508 function setupMutationObserver ( ) {
1509+ debugLog ( 'Setting up MutationObserver' ) ;
14371510 const observer = new MutationObserver ( debounce ( ( mutations ) => {
1511+ debugLog ( 'DOM changes detected:' , mutations ) ;
14381512 for ( const mutation of mutations ) {
14391513 if ( mutation . addedNodes . length ) {
1514+ debugLog ( 'New nodes added, triggering addCommitLabels' ) ;
14401515 addCommitLabels ( ) ;
14411516 }
14421517 }
@@ -1448,11 +1523,13 @@ SOFTWARE.
14481523 subtree : true
14491524 } ) ;
14501525
1526+ debugLog ( 'MutationObserver setup complete' ) ;
14511527 return observer ;
14521528 }
14531529
14541530 // Initialize the script
14551531 function initialize ( ) {
1532+ debugLog ( 'Initializing GitHub Commit Labels' ) ;
14561533 // Initial run
14571534 addCommitLabels ( ) ;
14581535
@@ -1461,6 +1538,7 @@ SOFTWARE.
14611538
14621539 // Clean up on page unload
14631540 window . addEventListener ( 'unload' , ( ) => {
1541+ debugLog ( 'Cleaning up on page unload' ) ;
14641542 observer . disconnect ( ) ;
14651543 } ) ;
14661544 }
0 commit comments