Skip to content

Commit

Permalink
Fix bug that URL won't be shortend in some pages (window.onload is …
Browse files Browse the repository at this point in the history
…untrustworthy)
  • Loading branch information
r7kamura committed May 16, 2022
1 parent 012f83b commit ccb9ff0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixed

- Fix bug that URL won't be shortend in some pages (`window.onload` is untrustworthy).

## 1.2.0 - 2022-05-17

### Added
Expand Down
1 change: 1 addition & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"https://www.amazon.se/*",
"https://www.amazon.sg/*"
],
"run_at": "document_end",
"js": ["src/main.ts"]
}
]
Expand Down
25 changes: 12 additions & 13 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ function replaceState(path: string) {
window.history.replaceState({}, "", path);
}

window.onload = () => {
const asinElement = document.querySelector(
'#ASIN, input[name="idx.asin"], input[name="ASIN.0"], input[name="titleID"]'
) as HTMLInputElement;
if (asinElement) {
replaceState(`/dp/${asinElement.value}`);
} else {
const href = window.location.href;
const match = href.match(/\/(?:dp|gp\/product)\/([A-Z0-9]{10})[/?]?/);
if (match) {
replaceState(`/dp/${match[1]}`);
}
const asinElement = document.querySelector(
'#ASIN, input[name="idx.asin"], input[name="ASIN.0"], input[name="titleID"]'
) as HTMLInputElement;

if (asinElement) {
replaceState(`/dp/${asinElement.value}`);
} else {
const href = window.location.href;
const match = href.match(/\/(?:dp|gp\/product)\/([A-Z0-9]{10})[/?]?/);
if (match) {
replaceState(`/dp/${match[1]}`);
}
};
}

0 comments on commit ccb9ff0

Please sign in to comment.