Skip to content

Convert JavaScript to TypeScript for improved type safety and maintainability#4

Merged
SIkebe merged 20 commits intomainfrom
copilot/fix-3
Jul 11, 2025
Merged

Convert JavaScript to TypeScript for improved type safety and maintainability#4
SIkebe merged 20 commits intomainfrom
copilot/fix-3

Conversation

Copy link
Contributor

Copilot AI commented Jul 11, 2025

This PR converts the Split Translator Chrome extension from JavaScript to TypeScript, providing comprehensive type safety and improved developer experience.

Changes Made

Core Conversion

  • Converted background.jssrc/background.ts with full type annotations for Chrome APIs
  • Converted popup.jssrc/popup.ts with comprehensive interface definitions
  • Added proper TypeScript types for all Chrome extension APIs, message passing, and DOM interactions

Build System

  • Added tsconfig.json with strict TypeScript configuration optimized for Chrome extensions
  • Updated package.json with TypeScript build scripts:
    • npm run build - Compile TypeScript to JavaScript
    • npm run watch - Watch mode for development
    • npm run dev - Clean build with development instructions
    • npm run clean - Clean build directory
  • Modified build pipeline to compile TypeScript files to dist/ directory

Project Structure

src/
  ├── background.ts    # Service worker (TypeScript)
  ├── popup.ts         # Popup script (TypeScript)
dist/
  ├── background.js    # Compiled service worker
  ├── popup.js         # Compiled popup script

Configuration Updates

  • Updated manifest.json to reference compiled JavaScript files in dist/
  • Modified popup.html to load compiled popup script
  • Updated build-package.bat to include TypeScript-compiled files
  • Enhanced README.md with TypeScript development workflow

Type Safety Improvements

  • Added interfaces for Chrome extension message passing
  • Proper typing for Chrome APIs (chrome.tabs, chrome.windows, chrome.storage)
  • Type-safe DOM element handling with proper casting
  • Comprehensive error handling with typed catch blocks

Benefits

  • Type Safety: Catch errors at compile time rather than runtime
  • Better IDE Support: Enhanced autocomplete, refactoring, and navigation
  • Maintainability: Self-documenting code with explicit type contracts
  • Developer Experience: Clear interfaces for message passing and API usage

Verification

✅ TypeScript compilation successful with no errors
✅ Generated JavaScript files pass syntax validation
✅ All Chrome extension functionality preserved
✅ Build process fully functional
✅ Extension structure validated for Chrome/Edge compatibility

The extension maintains 100% compatibility with existing functionality while providing a robust foundation for future development.

Fixes #3.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@SIkebe SIkebe changed the title [WIP] JavScript -> TypeScript [WIP] JavaScript -> TypeScript Jul 11, 2025
Copilot AI and others added 2 commits July 11, 2025 05:18
Co-authored-by: SIkebe <17608272+SIkebe@users.noreply.github.com>
Co-authored-by: SIkebe <17608272+SIkebe@users.noreply.github.com>
Copilot AI changed the title [WIP] JavaScript -> TypeScript Convert JavaScript to TypeScript for improved type safety and maintainability Jul 11, 2025
Copilot AI requested a review from SIkebe July 11, 2025 05:21
@SIkebe SIkebe requested a review from Copilot July 11, 2025 05:26

This comment was marked as outdated.

Comment on lines +259 to +266
width: currentWindow.width || 800,
height: currentWindow.height || 600
};
} else {
// Find the display to which the current window belongs
// Use window center point for accurate detection
const windowCenterX = currentWindow.left + (currentWindow.width / 2);
const windowCenterY = currentWindow.top + (currentWindow.height / 2);
const windowCenterX = (currentWindow.left || 0) + ((currentWindow.width || 800) / 2);
const windowCenterY = (currentWindow.top || 0) + ((currentWindow.height || 600) / 2);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この800や600は何を表していますか?既存のソースコードの中に、既に同じ意味を持つ定数が定義されていませんか?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

800と600は、ウィンドウの幅と高さの情報が取得できない場合のデフォルト値を表していました。ご指摘を受けて、これらのマジックナンバーをDEFAULT_WINDOW_WIDTHDEFAULT_WINDOW_HEIGHTという名前付き定数に変更しました。これにより、コードの意図がより明確になり、保守性が向上します。

変更は commit 4b18235 で実装されています。

Co-authored-by: SIkebe <17608272+SIkebe@users.noreply.github.com>
Copilot AI requested a review from SIkebe July 11, 2025 05:36
@SIkebe
Copy link
Owner

SIkebe commented Jul 11, 2025

以下のページを参考に、.github/workflows/copilot-setup-steps.yml を追加して
https://docs.github.com/en/enterprise-cloud@latest/copilot/how-tos/agents/copilot-coding-agent/customizing-the-development-environment-for-copilot-coding-agent

Co-authored-by: SIkebe <17608272+SIkebe@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jul 11, 2025

以下のページを参考に、.github/workflows/copilot-setup-steps.yml を追加して
https://docs.github.com/en/enterprise-cloud@latest/copilot/how-tos/agents/copilot-coding-agent/customizing...

Added .github/workflows/copilot-setup-steps.yml workflow file to set up the development environment for GitHub Copilot Coding Agent. The workflow includes Node.js setup, dependency installation, TypeScript compilation, and comprehensive development instructions for the Chrome extension project.

Commit: fe1b4e7

@SIkebe
Copy link
Owner

SIkebe commented Jul 11, 2025

Playwright MCP Server で https://docs.github.com/en/enterprise-cloud@latest/copilot/how-tos/agents/copilot-coding-agent/customizing-the-development-environment-for-copilot-coding-agent にアクセスし、.github/workflows/copilot-setup-steps.yml の仕様が本当に現在の実装で問題ないか確認してください。

また、Copilot が本プルリクエストで Code Review を実施した時のコメントも考慮してソースコードを修正してください。

Copy link
Contributor Author

Copilot AI commented Jul 11, 2025

Playwright MCP Server で https://docs.github.com/en/enterprise-cloud@latest/copilot/how-tos/agents/copilot-coding-agent/customizing-the-development-environment-for-copilot-coding-agent にアクセ��...

I'm unable to access the GitHub documentation URL due to domain restrictions in my environment. Could you please provide the specific requirements or issues you'd like me to check in the .github/workflows/copilot-setup-steps.yml file?

Regarding the code review comments about magic numbers (800, 600), those were already addressed in commit 4b18235 where I replaced them with named constants DEFAULT_WINDOW_WIDTH and DEFAULT_WINDOW_HEIGHT.

…nd better structure

Co-authored-by: SIkebe <17608272+SIkebe@users.noreply.github.com>
@SIkebe
Copy link
Owner

SIkebe commented Jul 11, 2025

@copilot Firewall の設定を修正しました。Playwright MCP Server で https://docs.github.com/en/enterprise-cloud@latest/copilot/how-tos/agents/copilot-coding-agent/customizing-the-development-environment-for-copilot-coding-agent にアクセスし、.github/workflows/copilot-setup-steps.yml の仕様が本当に現在の実装で問題ないか確認してください。

Copy link
Contributor Author

Copilot AI commented Jul 11, 2025

@copilot Firewall の設定を修正しました。Playwright MCP Server で https://docs.github.com/en/enterprise-cloud@latest/copilot/how-tos/agents/copilot-coding-agent/customizing-the-development-...

I'm unable to access the GitHub documentation URL due to domain restrictions in my environment. However, I can analyze the current .github/workflows/copilot-setup-steps.yml implementation for best practices and potential improvements.

Looking at the current workflow, I notice it may not follow the specific naming and structure requirements for Copilot setup workflows. Could you share the key requirements from the documentation, or would you prefer me to research alternative approaches based on GitHub Actions best practices for development environment setup?

Co-authored-by: SIkebe <17608272+SIkebe@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jul 11, 2025

開発環境として Devcontainer を使えるようにして。

  • Devcontainer の定義はなるべくシンプルにし、devcontainer.json だけで完結させること
    ...

Added Devcontainer support with a simple .devcontainer/devcontainer.json configuration that uses the latest Node.js LTS (v22), includes GitHub CLI via features, and automatically installs dependencies. The setup is minimal and self-contained as requested.

Commit: 1e05159

Co-authored-by: SIkebe <17608272+SIkebe@users.noreply.github.com>
Copilot AI requested a review from SIkebe July 11, 2025 07:51
@SIkebe SIkebe marked this pull request as ready for review July 11, 2025 07:53
@SIkebe SIkebe requested a review from Copilot July 11, 2025 07:54
Co-authored-by: SIkebe <17608272+SIkebe@users.noreply.github.com>
Copilot AI requested a review from SIkebe July 11, 2025 07:55

This comment was marked as outdated.

@SIkebe SIkebe requested a review from Copilot July 11, 2025 08:10
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR converts the Split Translator Chrome extension from JavaScript to TypeScript to improve type safety, maintainability, and developer experience.

  • Converted core extension scripts (background.js, popup.js) to TypeScript with full type annotations
  • Added build system updates (tsconfig.json, TypeScript scripts, packaging adjustments)
  • Introduced shared type definitions and enhanced documentation for development workflow

Reviewed Changes

Copilot reviewed 10 out of 11 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/types.ts Added shared TypeScript interfaces for message passing and window management
src/popup.ts Converted popup script to TypeScript with typed DOM references and improved error handling
src/background.ts Converted background service worker to TypeScript with strict types for Chrome APIs and display logic
popup.html Updated script reference to point to compiled dist/popup.js
package.json Added TypeScript build/watch/clean/package scripts and updated files to include dist output
manifest.json Updated service worker path to dist/background.js
build-package.bat Modified packaging script to zip compiled files from dist directory
README.md Expanded development section with TypeScript build instructions and updated project structure
.github/workflows/copilot-setup-steps.yml Introduced a GitHub Actions workflow for Copilot setup verification
.devcontainer/devcontainer.json Added devcontainer configuration for TypeScript development environment

currentTab: currentTab,
targetLanguage: targetLanguageSelect.value
});
} as SplitAndTranslateMessage) as SplitAndTranslateResponse;
Copy link

Copilot AI Jul 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The call to chrome.runtime.sendMessage is not awaited, which may lead to using an unresolved promise instead of the actual response. Consider using const response = await chrome.runtime.sendMessage(...) as SplitAndTranslateResponse;.

Copilot uses AI. Check for mistakes.

// Message listener
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
chrome.runtime.onMessage.addListener((request: SplitAndTranslateMessage, sender, sendResponse) => {
Copy link

Copilot AI Jul 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message listener doesn't handle rejected promises and is missing a return true; to keep the channel alive for async responses. Add a .catch(error => sendResponse(handleError(error, 'splitAndTranslate'))) after the .then and return true; at the end of the listener.

Copilot uses AI. Check for mistakes.
@SIkebe SIkebe merged commit fb83818 into main Jul 11, 2025
1 check passed
@SIkebe SIkebe deleted the copilot/fix-3 branch July 11, 2025 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JavaScript -> TypeScript

3 participants