-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring and packaging for Firefox add-ons website
- Loading branch information
1 parent
07d6c51
commit 1417042
Showing
10 changed files
with
153 additions
and
121 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules/** | ||
chrome-webstore-release.zip | ||
firefox-webstore-release.zip |
This file contains 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 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 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 |
---|---|---|
@@ -1,94 +1,91 @@ | ||
/* global chrome */ | ||
|
||
const compress = (text) => text.replace(/\s+/g, '') | ||
// Don't process HTTP response bodies over 30MB | ||
const MAX_BODY_SIZE_BYTES = 30 * 1024 * 1024 | ||
|
||
const maxBodyLength = 3000000 // 3MB | ||
|
||
const style = compress(` | ||
pre { | ||
display:none | ||
} | ||
#promformat { | ||
font-family: monospace; | ||
word-wrap: break-word; | ||
white-space: pre-wrap; | ||
} | ||
.comment { | ||
color: #6a737d; | ||
display: inline-block; | ||
} | ||
br + .comment { | ||
padding-top: 1em; | ||
} | ||
.comment + br + .comment { | ||
padding-top: 0; | ||
} | ||
.metric { color: #000 } | ||
.value { color: #ff20ed } | ||
.label-key { color: blue } | ||
.label-value { color: green } | ||
`) | ||
|
||
const port = chrome.runtime.connect({ name: 'promformat' }) | ||
|
||
// Add listener to receive response from BG when ready | ||
port.onMessage.addListener(msg => { | ||
switch (msg.name) { | ||
case 'FORMATTED' : | ||
// Insert CSS | ||
const promformatStyle = document.createElement('style') | ||
document.head.appendChild(promformatStyle) | ||
promformatStyle.insertAdjacentHTML('beforeend', style) | ||
|
||
// Insert HTML content | ||
const promformatContent = document.createElement('div') | ||
promformatContent.id = 'promformat' | ||
document.body.appendChild(promformatContent) | ||
|
||
promformatContent.innerHTML = msg.payload | ||
break | ||
|
||
default : | ||
throw new Error(`Message not understood: ${msg.name}`) | ||
} | ||
}) | ||
|
||
function ready (data) { | ||
const sendBodyToFormatter = (storedData) => { | ||
// Check if it is a Prometheus plain text response | ||
// This is quite a basic assumption, as the browser cannot access the | ||
// 'version' part of the content type to verify. | ||
const paths = data.paths.length ? data.paths : [] | ||
|
||
if (document.contentType !== 'text/plain') { | ||
port.disconnect() | ||
return | ||
} | ||
|
||
for (var i = 0; i < paths.length; ++i) { | ||
if (document.location.pathname.match(paths[i])) { | ||
format() | ||
break | ||
} | ||
// Check if the current page's paths matches one of our whitelist | ||
if (!storedData.paths.some(path => document.location.pathname.match(path))) { | ||
port.disconnect() | ||
return | ||
} | ||
} | ||
|
||
function format () { | ||
// Check if plain text wrapped in <pre> element exists and doesn't exceed maxBodyLength | ||
// Check if plain text wrapped in <pre> element exists and doesn't exceed | ||
// MAX_BODY_SIZE_BYTES | ||
const pre = document.body.querySelector('pre') | ||
const rawBody = pre && pre.innerText | ||
|
||
if (!rawBody || rawBody.length > maxBodyLength) { | ||
if (!rawBody || rawBody.length > MAX_BODY_SIZE_BYTES) { | ||
port.disconnect() | ||
return | ||
} | ||
|
||
// Post the contents of the PRE | ||
port.postMessage({ | ||
name: 'SENDING TEXT', | ||
name: 'PROMETHEUS_METRICS_RAW_BODY', | ||
payload: rawBody | ||
}) | ||
} | ||
|
||
const renderFormattedHTML = (html) => { | ||
const style = ` | ||
pre { | ||
display:none | ||
} | ||
#promformat { | ||
font-family: monospace; | ||
word-wrap: break-word; | ||
white-space: pre-wrap; | ||
} | ||
.comment { | ||
color: #6a737d; | ||
display: inline-block; | ||
} | ||
br + .comment { | ||
padding-top: 1em; | ||
} | ||
.comment + br + .comment { | ||
padding-top: 0; | ||
} | ||
.metric { color: #000 } | ||
.value { color: #ff20ed } | ||
.label-key { color: blue } | ||
.label-value { color: green } | ||
` | ||
|
||
// Insert CSS | ||
const promformatStyle = document.createElement('style') | ||
document.head.appendChild(promformatStyle) | ||
promformatStyle.insertAdjacentHTML('beforeend', style) | ||
|
||
// Insert HTML content | ||
const promformatContent = document.createElement('div') | ||
promformatContent.id = 'promformat' | ||
document.body.appendChild(promformatContent) | ||
|
||
promformatContent.innerHTML = html | ||
} | ||
|
||
const port = chrome.runtime.connect({ name: 'promformat' }) | ||
|
||
// Add listener to receive response from background when ready | ||
port.onMessage.addListener(msg => { | ||
if (msg.name !== 'PROMETHEUS_METRICS_FORMATTED_BODY') { | ||
return | ||
} | ||
|
||
renderFormattedHTML(msg.payload) | ||
}) | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
chrome.storage.sync.get({ paths: [] }, ready) | ||
chrome.storage.sync.get({ paths: [] }, sendBodyToFormatter) | ||
}) |
This file contains 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "Prometheus Formatter", | ||
"version": "1.2.4", | ||
"version": "2.0.0", | ||
"manifest_version": 2, | ||
"description": "Makes plain Prometheus metrics easier to read.", | ||
"homepage_url": "https://github.com/fhemberger/prometheus-formatter", | ||
|
@@ -37,11 +37,5 @@ | |
"permissions": [ | ||
"activeTab", | ||
"storage" | ||
], | ||
"browser_specific_settings": { | ||
"gecko": { | ||
"id": "[email protected]", | ||
"strict_min_version": "72.0" | ||
} | ||
} | ||
} | ||
] | ||
} |
This file contains 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 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,20 @@ | ||
#!/usr/bin/env bash | ||
set -uo pipefail | ||
|
||
readonly DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" | ||
|
||
function package_zip () { | ||
cd "$1" && zip -r "$2" html icons js LICENSE.txt manifest.json -x icons/.DS_Store js/.DS_Store .DS_Store | ||
} | ||
|
||
# Update manifest version number from package.json | ||
"$DIR/update-manifest-version.js" | ||
|
||
# Package Chrome webstore release | ||
package_zip "$DIR/../extension" "$DIR/../chrome-webstore-release.zip" | ||
|
||
# Package Firefox webstore release | ||
cp -r "$DIR/../extension" "$DIR/../extension-firefox" | ||
"$DIR/update-manifest-firefox.js" | ||
package_zip "$DIR/../extension-firefox" "$DIR/../firefox-webstore-release.zip" | ||
rm -r "$DIR/../extension-firefox" |
This file contains 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,16 @@ | ||
#!/usr/bin/env node | ||
|
||
const fs = require('fs') | ||
const path = require('path') | ||
const manifestPath = path.join(__dirname, '..', 'extension-firefox', 'manifest.json') | ||
const manifest = require(manifestPath) | ||
|
||
// Firefox needs additional keys in the manifest.json which are not allowed in Chrome | ||
manifest.browser_specific_settings = { | ||
gecko: { | ||
id: '[email protected]', | ||
strict_min_version: '72.0' | ||
} | ||
} | ||
|
||
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2), 'utf8') |
This file contains 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,10 @@ | ||
#!/usr/bin/env node | ||
|
||
const fs = require('fs') | ||
const path = require('path') | ||
const pkg = require(path.join(__dirname, '..', 'package.json')) | ||
const manifestPath = path.join(__dirname, '..', 'extension', 'manifest.json') | ||
const manifest = require(manifestPath) | ||
|
||
manifest.version = pkg.version | ||
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2), 'utf8') |
This file was deleted.
Oops, something went wrong.