Skip to content
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

Updated docs #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,32 @@
[![Build Status](https://travis-ci.org/announce/crx-mortal.svg?branch=master)](https://travis-ci.org/announce/crx-mortal)


## Getting Started
## Getting started

1. Install the Chrome Extension at [**Chrome Web Store**](https://chrome.google.com/webstore/detail/crx-mortal/plbhlfecmbmkphfgcpoijlidjapddidj?utm_source=github)
1. Install the extension at [**Chrome Web Store**](https://chrome.google.com/webstore/detail/crx-mortal/fgnhcpkfpdiaeikgdjbgfodclmnpbjno?utm_source=github)
2. Submit your game log at https://mjai.ekyu.moe/

## How crx-mortal works

This extension annotates your less optimal moves based on the Mortal's score where `πτ​(a∣s)×100 < 5.0`.

## Development

Prerequisites:

* Docker
* Node v18
* `jq` command

Run:
To run in the development mode:

```bash
./app start
npm run watch
```

For the release:
To release the extension:

```bash
./app release
npm run build
```

## Project Links

* Upstream work: [trunk](https://github.com/announce/crx-mortal/compare/master...ymkjp:master)
* Demo app: [crx](https://chrome.google.com/webstore/detail/crx-mortal/plbhlfecmbmkphfgcpoijlidjapddidj?utm_source=github)
* Release: [Chrome Developer Dashboard](https://chrome.google.com/webstore/developer/dashboard)

## Documents

* CRX
* [Declare Permissions](https://developer.chrome.com/extensions/declare_permissions)
* [chrome\.i18n](https://developer.chrome.com/extensions/i18n)
* ImageMagick
* [Text Handling \- IM v6](http://www.imagemagick.org/Usage/text/#label_bestfit)
Then submit the bundled package at [**Chrome Developer Dashboard**](https://chrome.google.com/webstore/developer/dashboard).
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"name": "crx-mortal",
"version": "1.0.0",
"description": "crx-mortal",
"main": "index.js",
"version": "1.0.1",
"description": "A Chrome extension to help Mahjong players spot their less optimal moves",
"scripts": {
"watch": "webpack --config webpack/webpack.dev.js --watch",
"build": "npm-run-all style clean build:** bundle",
"build:manifest": "./app update-manifest-version",
"build:js": "webpack --config webpack/webpack.prod.js",
"bundle": "zip -r dist-$(date +%s).zip ./dist/*",
"bundle": "./app bundle-dist",
"clean": "rimraf dist",
"test": "npx jest",
"style": "prettier --write \"src/**/*.{ts,tsx}\" \"public/**/*.{json,html,css}\""
},
"author": "",
"author": "Kenta Yamamoto <[email protected]>",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
16 changes: 9 additions & 7 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
{
"manifest_version": 3,

"name": "crx-mortal",
"description": "annotating bad-moves",
"version": "1.0",

"description": "This extension helps Mahjong players spot their less optimal moves",
"version": "1.0.1",
"action": {
"default_icon": "icon.png"
},

"content_scripts": [
{
"matches": ["https://mjai.ekyu.moe/*"],
"js": ["js/vendor.js", "js/content_script.js"]
"matches": [
"https://mjai.ekyu.moe/*"
],
"js": [
"js/vendor.js",
"js/content_script.js"
]
}
]
}
20 changes: 8 additions & 12 deletions script/app.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@
app () {
set -u

start () {
export NODE_ENV=development
npm start
bundle-dist () {
[[ $(command -v zip) ]] || return
zip -r ./var/dist-$(date +%s).zip ./dist/*
}

release () {
export NODE_ENV=production
npm run build
}

die () {
MESSAGE="${1:-Something went wrong.}"
echo "[$(basename "$0")] ERROR: ${MESSAGE}" >&2
exit 1
update-manifest-version () {
[[ $(command -v jq) ]] || return
PACKAGE_VERSION="$(jq '.version' package.json)"
MANIFEST="$(jq ".version = ${PACKAGE_VERSION}" public/manifest.json)"
echo -e "${MANIFEST}" > public/manifest.json
}

usage () {
Expand Down
92 changes: 50 additions & 42 deletions src/content_script.tsx
Original file line number Diff line number Diff line change
@@ -1,48 +1,56 @@
/**
* https://mjai.ekyu.moe/report/888288d74f7ed24c.html#kyoku-6-0
*/
class CrxMortal {
static DEFAULT_THRESHOLD = 5.0;
createSpan = (text: string) => {
const span = document.createElement("span");
span.innerText = text;
span.style.background = `#ffd5d5`;
span.style.margin = `auto .5rem`;
span.style.fontWeight = `bold`;
return span;
};

const THRESHOLD = 5.0;
const createSpan = (text: string) => {
const suggestion = document.createElement("span");
suggestion.innerText = text;
suggestion.style.background = `#ffd5d5`;
suggestion.style.margin = `auto .5rem`;
suggestion.style.fontWeight = `bold`;
return suggestion;
};
main = () => {
const scores = [...document.querySelectorAll(`body section details.entry`)]
.map((element) => {
const tile = element.querySelector(`span svg.tile use`) as SVGAElement;
if (tile === null) {
return null;
}
// '#pai-9s'
const score = element
.querySelector(`table [href*="${tile.href.baseVal}"]`)
?.closest(`tr`)
?.querySelector(`td:nth-child(3)`) as HTMLElement;
const scoreNum = parseFloat(score.textContent || ``);
if (scoreNum < CrxMortal.DEFAULT_THRESHOLD) {
const suggestion = this.createSpan(score.textContent || ``);
element.querySelector(`:scope > span`)?.appendChild(suggestion);
} else {
element.removeAttribute(`open`);
}
return scoreNum;
})
.filter((score) => score !== null);

const scores = [...document.querySelectorAll(`body section details.entry`)]
.map((element) => {
const tile = element.querySelector(`span svg.tile use`) as SVGAElement;
if (tile === null) {
return null;
}
// '#pai-9s'
const score = element
.querySelector(`table [href*="${tile.href.baseVal}"]`)
?.closest(`tr`)
?.querySelector(`td:nth-child(3)`) as HTMLElement;
const scoreNum = parseFloat(score.textContent || ``);
if (scoreNum < THRESHOLD) {
const suggestion = createSpan(score.textContent || ``);
element.querySelector(`:scope > span`)?.appendChild(suggestion);
} else {
element.removeAttribute(`open`);
}
return scoreNum;
})
.filter((score) => score !== null);

const badMoves = scores.filter(
(score) => score !== null && score < THRESHOLD
).length;
const totalMoves = scores.length;

const metrics = createSpan(
`${badMoves}/${totalMoves} = ${Math.round((badMoves / totalMoves) * 100)}%`
);
document
.querySelector("body > details.collapse:nth-child(6) > summary")
?.appendChild(metrics);
const totalMoves = scores.length;
const badMoves = scores.filter(
(score) => score !== null && score < CrxMortal.DEFAULT_THRESHOLD
).length;
const metrics = this.createSpan(
`${badMoves}/${totalMoves} = ${
totalMoves > 0 ? Math.round((badMoves / totalMoves) * 100) : NaN
}%`
);
document
.querySelector("body > details.collapse:nth-child(6) > summary")
?.appendChild(metrics);
};
}

if (require.main === module) {
const crx = new CrxMortal();
crx.main();
}
2 changes: 1 addition & 1 deletion src/popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Popup = () => {
useEffect(() => {
(async () => {
await chrome.action.setBadgeText({ text: count.toString() });
})()
})();
}, [count]);

useEffect(() => {
Expand Down