-
Notifications
You must be signed in to change notification settings - Fork 9
Add Firefox support for CharacterSync extension #325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -197,84 +197,81 @@ | |
| } | ||
|
|
||
| /** | ||
| * Check if the "D&D Character Sync" extension is installed | ||
| * Send a message to the "D&D Character Sync" browser extension. | ||
| * Uses window.postMessage bridge on Firefox, chrome.runtime.sendMessage on Chrome. | ||
| * | ||
| * @param {string} url | ||
| * @param {string[]} request_content - The content keys to request (e.g. ["version"], ["characters"]) | ||
| * @returns {Promise<object|undefined>} The extension's response, or undefined on error/timeout | ||
| */ | ||
| export async function extensionInstalled() { | ||
| const sendMessage = new Promise((resolve) => { | ||
| function sendExtensionMessage(request_content) { | ||
| const isFirefox = navigator.userAgent.includes("Firefox"); | ||
|
|
||
| if (isFirefox) { | ||
| return new Promise((resolve) => { | ||
| const requestId = Math.random().toString(36).slice(2); | ||
| function handler(event) { | ||
| if (!event.data?.CS_BRIDGE_RESPONSE || event.data.requestId !== requestId) return; | ||
| window.removeEventListener("message", handler); | ||
| resolve(event.data); | ||
| } | ||
| window.addEventListener("message", handler); | ||
| window.postMessage({ CS_BRIDGE: true, requestId, request_content }, "*"); | ||
Check failureCode scanning / SonarCloud Origins should be verified during cross-origin communications High
Specify a target origin for this message. See more on SonarQube Cloud
|
||
| }); | ||
| } | ||
|
|
||
| return new Promise((resolve) => { | ||
| chrome?.runtime?.sendMessage( | ||
| character_sync_id, | ||
| { request_content: ["version"] }, | ||
| { request_content }, | ||
| (response) => { | ||
| if (chrome.runtime.lastError) { | ||
| resolve(undefined); | ||
| return; | ||
| } | ||
| if (response) { | ||
| resolve(response.version); | ||
| } else { | ||
| resolve(undefined); | ||
| } | ||
| resolve(response); | ||
| } | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Check if the "D&D Character Sync" extension is installed | ||
| */ | ||
| export async function extensionInstalled() { | ||
| const sendMessage = sendExtensionMessage(["version"]).then( | ||
| (response) => response?.version | ||
| ); | ||
| const timeout = new Promise((resolve) => { | ||
| setTimeout(resolve, 2000, undefined); | ||
| }); | ||
| return Promise.race([sendMessage, timeout]); | ||
| } | ||
|
|
||
| /** | ||
| * Gets all characters from "D&D Character Sync" Chrome Extension | ||
| * Gets all characters from "D&D Character Sync" browser extension | ||
| */ | ||
| export async function getCharacterSyncStorage() { | ||
| const sendMessage = new Promise((resolve) => { | ||
| chrome?.runtime?.sendMessage( | ||
| character_sync_id, | ||
| { request_content: ["characters"] }, | ||
| (response) => { | ||
| if (chrome.runtime.lastError) { | ||
| resolve({}); | ||
| return; | ||
| } | ||
| if (response && response.characters) { | ||
| resolve(response.characters); | ||
| } else { | ||
| resolve({}); | ||
| } | ||
| } | ||
| ); | ||
| }); | ||
| const sendMessage = sendExtensionMessage(["characters"]).then( | ||
| (response) => response?.characters || {} | ||
| ); | ||
| const timeout = new Promise((resolve) => { | ||
| setTimeout(resolve, 2000, {}); | ||
| }); | ||
| return Promise.race([sendMessage, timeout]); | ||
| } | ||
|
|
||
| /** | ||
| * Get a single character from the "D&D Character Sync" Chrome Extension | ||
| * Get a single character from the "D&D Character Sync" browser extension | ||
| * | ||
| * @param {string} url | ||
| * @returns | ||
| */ | ||
| export async function getCharacterSyncCharacter(url) { | ||
| const sendMessage = new Promise((resolve, reject) => { | ||
| chrome?.runtime?.sendMessage( | ||
| character_sync_id, | ||
| { request_content: ["characters"] }, | ||
| (response) => { | ||
| if (chrome.runtime.lastError) { | ||
| reject(`Character not found in D&D Character Sync Extension`); | ||
| return; | ||
| } | ||
| if (response.characters && url in response.characters) { | ||
| resolve(response.characters[url]); | ||
| } else { | ||
| reject(`Character not found in D&D Character Sync Extension`); | ||
| } | ||
| } | ||
| ); | ||
| const sendMessage = sendExtensionMessage(["characters"]).then((response) => { | ||
| if (response?.characters && url in response.characters) { | ||
| return response.characters[url]; | ||
| } | ||
| throw `Character not found in D&D Character Sync Extension`; | ||
| }); | ||
| const timeout = new Promise((resolve) => { | ||
| setTimeout(resolve, 2000, `Character not found in D&D Character Sync Extension`); | ||
|
|
@@ -345,7 +342,7 @@ | |
|
|
||
| wrapper.appendChild(clone); | ||
| wrapper.appendChild(footer); | ||
|
|
||
| const canvas = await html2canvas(wrapper, { | ||
| scale: 2, | ||
| useCORS: true | ||
|
|
@@ -370,25 +367,25 @@ | |
| const pdf = new jsPDF("p", "mm", "a4"); | ||
| let pageWidth = pdf.internal.pageSize.getWidth(); | ||
| const pageHeight = pdf.internal.pageSize.getHeight(); | ||
|
|
||
| if (layout === "single-column") { | ||
| pageWidth = pageWidth / 2; | ||
| } | ||
|
|
||
| const contentWidth = pageWidth - margin * 2; | ||
| const contentHeight = (canvas.height * contentWidth) / canvas.width; | ||
|
|
||
| pdf.addImage(imgData, "PNG", margin, margin, contentWidth, contentHeight); | ||
|
|
||
| const pageCount = pdf.internal.getNumberOfPages(); | ||
| for (let i = 1; i <= pageCount; i++) { | ||
| pdf.setPage(i); | ||
| pdf.setFontSize(10); | ||
| pdf.setTextColor(100); | ||
| pdf.text(footerText, margin, pageHeight - margin, { align: 'left' }); | ||
| } | ||
|
|
||
| pdf.save(`${filename}.pdf`); | ||
| document.body.removeChild(clone); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / SonarCloud
Origins should be verified during cross-origin communications High