Skip to content

Commit

Permalink
Merge pull request #10 from big-mon/dev
Browse files Browse the repository at this point in the history
convert typescript
  • Loading branch information
big-mon authored Sep 8, 2021
2 parents f057246 + 558ddf5 commit 376274e
Show file tree
Hide file tree
Showing 22 changed files with 1,406 additions and 193 deletions.
14 changes: 0 additions & 14 deletions .github/workflows/lint.yml

This file was deleted.

4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ yarn
### Step 2: Deploy

```sh
git archive HEAD:src --output=../amazon-wishlist-pointgetter.zip
yarn build
```

deploy to `/dist`
22 changes: 16 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@
"version": "1.0.0",
"private": true,
"license": "MIT",
"repository": {
"type": "git",
"url": "git+ssh://[email protected]/big-mon/amazon-wishlist-pointgetter"
},
"repository": "https://github.com/big-mon/amazon-wishlist-pointgetter",
"author": "BIG-MON <> (https://github.com/big-mon)",
"scripts": {},
"devDependencies": {}
"scripts": {
"build": "webpack.cmd --mode production"
},
"devDependencies": {
"@types/chrome": "^0.0.157",
"@types/copy-webpack-plugin": "^8.0.1",
"@types/webpack": "^5.28.0",
"copy-webpack-plugin": "^9.0.1",
"ts-loader": "^9.2.5",
"ts-node": "^10.2.1",
"typescript": "^4.4.2",
"webpack": "^5.52.0",
"webpack-cli": "^4.8.0"
},
"dependencies": {}
}
5 changes: 5 additions & 0 deletions public/_locales/en/messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"appDesc": {
"message": "Add point return labels in Amazon.co.jp Wishlist and Ranking Page."
}
}
5 changes: 5 additions & 0 deletions public/_locales/ja/messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"appDesc": {
"message": "Amazon.co.jpのウィッシュリストとランキングページに獲得できるポイントの表示を追加。"
}
}
File renamed without changes
File renamed without changes
File renamed without changes
10 changes: 3 additions & 7 deletions src/manifest.json → public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 3,
"name": "Devola: Amazon Wishlist Point Visualization",
"short_name": "Devola",
"version": "1.2.0",
"version": "1.3.0",
"description": "__MSG_appDesc__",
"default_locale": "ja",
"icons": {
Expand All @@ -20,12 +20,8 @@
"https://www.amazon.co.jp/gp/movers-and-shakers/*"
],
"css": [],
"js": [
"lib/jquery.js",
"js/functionOrder.js",
"js/wishlist.js",
"js/ranking.js"
]
"js": ["js/base.js", "js/util.js", "js/ranking.js", "js/wishlist.js"],
"run_at": "document_end"
}
]
}
5 changes: 0 additions & 5 deletions src/_locales/en/messages.json

This file was deleted.

5 changes: 0 additions & 5 deletions src/_locales/ja/messages.json

This file was deleted.

16 changes: 16 additions & 0 deletions src/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { doWishlist } from "./wishlist";
import { doRanking } from "./ranking";

/** 処理全体のスターター */
const start = () => {
let url = location.href;

// [ウィッシュリスト]ページの場合
if (url.indexOf("wishlist") != -1) {
doWishlist();
} else {
doRanking();
}
};

start();
19 changes: 0 additions & 19 deletions src/js/functionOrder.js

This file was deleted.

56 changes: 0 additions & 56 deletions src/js/ranking.js

This file was deleted.

78 changes: 0 additions & 78 deletions src/js/wishlist.js

This file was deleted.

2 changes: 0 additions & 2 deletions src/lib/jquery.js

This file was deleted.

36 changes: 36 additions & 0 deletions src/ranking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { fetchPoints } from "./util";

/** ランキング上の商品ブロックを走査 */
export const doRanking = () => {
const domain = location.protocol + "//" + location.host;

const allItems = document
.getElementById("zg-ordered-list")
?.querySelectorAll("li");

allItems?.forEach((value, _index) => {
editItem(value, domain);
});
};

/** 商品ブロックに取得ポイントを追記 */
const editItem = async (item: HTMLElement, domain: string) => {
// 商品のURLを取得
const selectorUrl = ".zg-item .a-link-normal";
const url = domain + item.querySelector(selectorUrl)?.getAttribute("href");

// 商品の取得ポイントを取得
const result = await fetchPoints(url);

// 価格要素を取得
const selectorPrice = ".zg-item .p13n-sc-price:last-child";
const priceTag = item.querySelector(selectorPrice);
if (priceTag == null) return;

// 価格要素に取得ポイントを追記
const pointTag =
'<span class="add-point a-size-small" style="margin-left: .6rem;"><span class="a-color-price">' +
result +
"</span></span>";
priceTag.insertAdjacentHTML("beforeend", pointTag);
};
37 changes: 37 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/** 商品URLから取得ポイントを取得 */
export const fetchPoints = async (url: string) => {
const response = await fetch(url);
const resData = await response.text();
return parsePoints(resData);
};

/** 商品ページからポイント部分を取得 */
const parsePoints = (data: string) => {
// セレクター
const normalItem1 =
"#addToCart #pointsInsideBuyBox_feature_div span.a-color-price";
const normalItem2 =
"#addToCart #buyBoxInner span.a-color-price:not(.offer-price)";
const kindleItem = ".loyalty-points .a-align-bottom";

// 取得ポイント部分のDOM
const dom = new DOMParser()
.parseFromString(data, "text/html")
.querySelector(`${normalItem1},${normalItem2},${kindleItem}`);
if (!dom || !dom.textContent) return "";

return escapeHtml(trimText(dom.textContent));
};

/** 文字列エスケープ */
const escapeHtml = (unsafe: string) =>
unsafe
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");

/** 文字列トリム */
const trimText = (text: string) =>
text.replace(/\t/g, "").replace(/ /g, "").replace(/\r?\n/g, "");
Loading

0 comments on commit 376274e

Please sign in to comment.