-
Notifications
You must be signed in to change notification settings - Fork 4
Automates the creation of the extension #20
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d5a80d4
Fixing version of dependencies.
armfazh e9f550b
Generating crx extension and assets.
armfazh 5a75274
Fix set of main dependencies
armfazh f8b54b2
Ensure libsodium has been initialized.
armfazh 3157b1d
Fixing dependencies in workspace.
armfazh 326d8cd
Update label in templates.
armfazh 4109ffa
Removing libsodium ready.
armfazh 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
This file was deleted.
Oops, something went wrong.
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
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
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
2 changes: 1 addition & 1 deletion
2
...ser-extension/config/chromium/policy.json → ...rowser-extension/policy/policy.json.templ
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
93 changes: 93 additions & 0 deletions
93
examples/browser-extension/scripts/build_web_artifacts.mjs
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2025 Cloudflare, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import ChromeExtension from "crx"; | ||
import * as fs from "node:fs"; | ||
import path from "node:path"; | ||
const { KeyObject } = await import("node:crypto"); | ||
const { subtle } = globalThis.crypto; | ||
import pkg from '../package.json' with { type: "json" }; | ||
|
||
function makePolicy(extensionID) { | ||
const MarkerString = "EXTENSION_ID_REPLACED_BY_NPM_RUN_BUNDLE_CHROME" | ||
const policyPath = path.join(path.dirname("."), "policy"); | ||
if (!fs.existsSync(policyPath)) { | ||
fs.mkdirSync(policyPath, { recursive: true }); | ||
} | ||
|
||
for (let fileName of ["com.google.Chrome.managed.plist", "policy.json"]) { | ||
const template = fs.readFileSync( | ||
path.join(policyPath, fileName + ".templ"), | ||
"utf8" | ||
); | ||
const fileContent = template.split(MarkerString).join(extensionID); | ||
fs.writeFileSync(path.join(policyPath, fileName), fileContent); | ||
} | ||
} | ||
|
||
function setManifestVersion(version) { | ||
const manifestInputPath = path.join(path.dirname("."), "platform", "mv3", "chromium", 'manifest.json'); | ||
const manifestOutputPath = path.join(path.dirname("."), "dist", "mv3", "chromium", 'manifest.json'); | ||
const manifestStr = fs.readFileSync(manifestInputPath, "utf8"); | ||
const manifest = JSON.parse(manifestStr); | ||
manifest.version = version; | ||
fs.writeFileSync(manifestOutputPath, JSON.stringify(manifest, null, 2)); | ||
} | ||
|
||
async function main() { | ||
const distPath = path.join(path.dirname("."), "dist", "web-ext-artifacts"); | ||
if (!fs.existsSync(distPath)) { | ||
fs.mkdirSync(distPath, { recursive: true }); | ||
} | ||
|
||
const { privateKey, publicKey } = await subtle.generateKey( | ||
{ | ||
name: "RSASSA-PKCS1-v1_5", | ||
modulusLength: 2048, | ||
publicExponent: Uint8Array.from([1, 0, 1]), | ||
hash: "SHA-256", | ||
}, | ||
true, | ||
["sign", "verify"] | ||
); | ||
|
||
const skPEM = KeyObject.from(privateKey).export({ | ||
type: "pkcs8", | ||
format: "pem", | ||
}); | ||
const pkBytes = KeyObject.from(publicKey).export({ | ||
type: "pkcs1", | ||
format: "der", | ||
}); | ||
|
||
const crx = new ChromeExtension({ | ||
codebase: "http://localhost:8000/" + pkg.name + '.crx', | ||
privateKey: skPEM, | ||
publicKey: pkBytes, | ||
}); | ||
|
||
setManifestVersion(pkg.version); | ||
await crx.load(path.join(path.dirname("."), "dist", "mv3", "chromium")) | ||
const extensionBytes = await crx.pack(); | ||
const extensionID = crx.generateAppId(); | ||
|
||
fs.writeFileSync("private_key.pem", skPEM); | ||
fs.writeFileSync(path.join(distPath, pkg.name + '.crx'), extensionBytes); | ||
fs.writeFileSync(path.join(distPath, "update.xml"), crx.generateUpdateXML()); | ||
makePolicy(extensionID); | ||
|
||
console.log(`Build Extension with ID: ${extensionID}`) | ||
}; | ||
|
||
await main(); |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.