Skip to content

Commit cc451d7

Browse files
committed
Refactor documentation in buttons-init.js, buttons-injection.js, and buttons.js for clarity and completeness
1 parent 5cec6ed commit cc451d7

File tree

3 files changed

+78
-33
lines changed

3 files changed

+78
-33
lines changed

buttons-init.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
1+
// buttons-init.js
12
// Version: 1.0
23
//
34
// Documentation:
4-
// This file handles the initialization of custom buttons and toggles within the ChatGPT extension.
5-
// It ensures that custom buttons and toggles are created and appended to the DOM without duplication.
5+
// Initialization and rendering of the *entire* buttons row for target containers (inline chat area
6+
// or the floating panel). Prevents duplicates, composes all button types, and appends global toggles.
7+
//
8+
// What this module renders (in order):
9+
// 1) Floating panel toggle (for inline containers only, if the panel feature exists)
10+
// 2) Inline Profile Selector — optional, position configurable ("before" or "after")
11+
// 3) A unified list of buttons:
12+
// - Cross-Chat buttons ("Copy", "Paste") placed "before" or "after" based on globalCrossChatConfig
13+
// - Custom buttons from globalMaxExtensionConfig.customButtons (honors separators)
14+
// - Numeric shortcuts (1–10) assigned to the first 10 non-separator buttons when enabled
15+
// 4) Global toggles appended last: "Auto-send" and "Hotkeys"
616
//
717
// Functions:
8-
// - createAndInsertCustomElements: Creates and inserts custom buttons and toggles into the target container.
9-
// - generateAndAppendToggles: Creates and appends toggle switches (e.g., Auto-send, Hotkeys) to a specified container.
10-
// - updateButtonsForProfileChange: Updates all buttons and toggles in response to a profile change.
18+
// - createAndInsertCustomElements(targetContainer): Creates the container, renders everything once, and inserts it.
19+
// Uses a dynamic id from InjectionTargetsOnWebsite to avoid duplication.
20+
// - generateAndAppendAllButtons(container, isPanel): Main renderer for all buttons + optional controls.
21+
// - generateAndAppendToggles(container): Appends Auto-send and Hotkeys toggles and wires them to global config.
22+
// - updateButtonsForProfileChange(origin): Scoped re-render — only "panel" or only "inline".
23+
// - createInlineProfileSelector(): Builds the inline profile <select>, loads profile list, styles for theme,
24+
// blocks event bubbling on hostile SPAs, and triggers a partial refresh on change.
25+
//
26+
// Notes:
27+
// - Cross-Chat placement and autosend behavior is driven by window.globalCrossChatConfig.
28+
// - Shortcut keys appear in tooltips; Shift+click inversion happens in processCustomSendButtonClick.
1129
//
1230
// Usage:
13-
// Ensure that `buttons.js` and `init.js` are loaded before this script to utilize button initialization functionalities.
14-
// This script should be included in the `content_scripts` section of the manifest to be injected into the target pages.
31+
// Ensure `buttons.js`, `utils.js`, and `init.js` are loaded first. Include in `content_scripts` so it runs
32+
// on the target pages. This module only performs DOM composition; click routing is in buttons.js.
33+
//
1534
// Instructions for AI: do not remove comments! MUST NOT REMOVE COMMENTS. This one too!
1635
'use strict';
1736

buttons-injection.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
'use strict';
2-
// button-injection.js version 1.1
1+
"use strict";
2+
// buttons-injection.js version 1.1
33
/**
4-
* Button Injection Logic for the ChatGPT Chrome extension.
5-
* This file contains functions to handle custom button injection into the webpage,
6-
* including resiliency checks and re-initialization measures.
4+
* Button Injection:
5+
* Detects target containers, injects the buttons UI once, and maintains it across SPA navigation
6+
* via adaptive resiliency checks and long-running DOM observation.
77
*/
88
//Instructions for AI: do not remove comments! MUST NOT REMOVE COMMENTS. This one too!
99
// ALL CODE IN ALL FILES MUST USE logConCgp FOR LOGGING. NO CONSOLE LOGGING.
@@ -15,6 +15,17 @@ const EXTENDED_CHECK_DURATION = 2 * 60 * 60 * 1000; // 2 hours in milliseconds
1515
// Flag to coordinate with the floating panel toggle logic.
1616
window.OneClickPrompts_isTogglingPanel = false;
1717

18+
/**
19+
* Summary of behavior:
20+
* - Loads persisted toggle states before any DOM work (MaxExtensionInterface.loadToggleStates()).
21+
* - Waits for any of the configured target containers (InjectionTargetsOnWebsite.selectors.containers).
22+
* - On first hit, calls MaxExtensionButtonsInit.createAndInsertCustomElements(targetDiv).
23+
* - Starts an adaptive watchdog loop that re-injects when the UI disappears (e.g., SPA route changes).
24+
* - Starts a MutationObserver for extended monitoring (up to 2 hours) to cheaply detect DOM wipes.
25+
* - Coordinates with floating panel toggling via window.OneClickPrompts_isTogglingPanel to avoid races.
26+
* - Stops event propagation on the inline profile <select> to prevent site handlers from closing it.
27+
*/
28+
1829
/**
1930
* Checks whether the custom buttons modifications already exist in the DOM.
2031
* This is strengthened to check for child elements, not just the container.

buttons.js

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,40 @@
11
/* buttons.js
2-
Version: 1.0
3-
4-
Documentation:
5-
6-
This file is a dependency. Designed to host helper functions for init.js. Manages the creation and functionality of custom send buttons within the ChatGPT extension.
7-
It provides utility functions to create buttons based on configuration and assigns keyboard shortcuts where applicable.
8-
9-
After that, tje
10-
11-
Usage:
12-
Ensure that dependencies are loaded before this script to utilize button functionalities.
13-
14-
Depends on:
15-
utils.js - object containing all selectors and identifiers
16-
buttons-init.js - handles only some initializations.
17-
+
18-
buttons-clicking-chatgpt.js - handles the send button clicking process for ChatGPT
19-
buttons-clicking-copilot.js - ... for Copilot
20-
buttons-clicking-claude.js - ... Claude
21-
22-
Instructions for AI: do not remove comments! MUST NOT REMOVE COMMENTS. This one too!
2+
Version: 1.0
3+
4+
Documentation:
5+
Button creation + click orchestration.
6+
- Builds both custom send buttons (from profile/global config) and Cross-Chat buttons ("Copy", "Paste").
7+
- Assigns numeric shortcuts (1–10) to the first 10 non-separator buttons (configurable via globalMaxExtensionConfig.enableShortcuts).
8+
- Composes titles that include autosend status and shortcut hints.
9+
- Handles click behavior across supported sites and integrates with queue mode in the floating panel.
10+
11+
Exposed methods:
12+
- MaxExtensionButtons.createCustomSendButton(buttonConfig, index, onClickHandler, overrideShortcutKey?)
13+
- MaxExtensionButtons.createCrossChatButton(type: 'copy'|'paste', shortcutKey?)
14+
- MaxExtensionButtons.determineShortcutKeyForButtonIndex(buttonIndex, offset?)
15+
16+
Click flow:
17+
- processCustomSendButtonClick(event, customText, autoSend)
18+
* Shift inverts autoSend at click time.
19+
* If the floating panel is visible and queue mode is enabled, the button is enqueued instead of sending immediately.
20+
* Routes to site-specific handlers based on InjectionTargetsOnWebsite.activeSite:
21+
- ChatGPT, Claude, Copilot, DeepSeek, AIStudio, Grok, Gemini
22+
23+
Cross-Chat notes:
24+
- "Copy": reads from the active editor, saves via service worker, briefly shows "Copied!" in tooltip,
25+
and triggers autosend with the existing text when configured.
26+
- "Paste": fetches stored prompt; tooltip shows a debounced preview on hover.
27+
28+
Usage:
29+
Load order should ensure `utils.js` and any site-specific clicking modules are present before use.
30+
Rendering order and placement are orchestrated by buttons-init.js; this file focuses on element creation and behavior.
31+
32+
Depends on:
33+
- utils.js (selectors and shared utilities)
34+
- buttons-init.js (composition/placement)
35+
- buttons-clicking-*.js (site handlers: chatgpt/claude/copilot/deepseek/aistudio/grok/gemini)
36+
37+
Instructions for AI: do not remove comments! MUST NOT REMOVE COMMENTS. This one too!
2338
*/
2439
'use strict';
2540

0 commit comments

Comments
 (0)