Skip to content

Commit 521b178

Browse files
authored
Add bookmarklet to the readme (#35)
* Add Etherscan link to status bar * Add tests for bookmarklet Yes, I know, it should be trivial, but I managed to have a bug in it. * Describe bookmarklet in the readme
1 parent 48de72b commit 521b178

File tree

8 files changed

+130
-3
lines changed

8 files changed

+130
-3
lines changed

.mocharc.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ module.exports = {
33
extension: ["ts"],
44
watchExtensions: ["ts"],
55
// Extension tests run inside of VSCode instance, so we don't include them here
6-
spec: ["packages/vscode-host/src/**/*.test.ts"],
6+
spec: [
7+
"packages/vscode-host/src/**/*.test.ts",
8+
"packages/bookmarklet/*.test.ts",
9+
],
710
};

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,19 @@ While browsing smart contract code on [Etherscan](https://etherscan.io/) just ch
2020

2121
![ecv](https://user-images.githubusercontent.com/1814312/146108385-6fa50ae7-14a5-45b2-be3d-201d22409cf7.gif)
2222

23+
Or save the following code snippet as a bookmarklet to quickly go from any [supported chain explorer][supported_explorers] to Ethereum Code Viewer.
24+
25+
```
26+
javascript: location.href = location.href.replace(/\.\w+(\/)/, ".deth.net/")
27+
```
28+
2329
## Features ⚡
2430

2531
- frictionless - just tweak URL while browsing etherscan `.io` -> `deth.net`
2632
- proxy support - automatically follows proxies and displays implementation source code
27-
- multichain - supports different etherscan instances: testnets, L2s, L1s ([all supported chains](https://github.com/dethcrypto/ethereum-code-viewer/blob/main/packages/ethereum-viewer/src/explorer/networks.ts))
33+
- multichain - supports different etherscan instances: testnets, L2s, L1s ([all supported chains][supported_explorers])
34+
35+
[supported_explorers]: https://github.com/dethcrypto/ethereum-code-viewer/blob/main/docs/supported-explorers.md
2836

2937
## Motivation
3038

docs/supported-explorers.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Ethereum Code Viewer supports the following blockchain explorers:
2+
3+
<!-- Run `yarn supported-explorers` in packages/ethereum-viewer to generate this list. -->
4+
5+
- [etherscan.io](https://etherscan.io)
6+
- [ropsten.etherscan.io](https://ropsten.etherscan.io)
7+
- [rinkeby.etherscan.io](https://rinkeby.etherscan.io)
8+
- [goerli.etherscan.io](https://goerli.etherscan.io)
9+
- [kovan.etherscan.io](https://kovan.etherscan.io)
10+
- [bscscan.com](https://bscscan.com)
11+
- [testnet.bscscan.com](https://testnet.bscscan.com)
12+
- [hecoinfo.com](https://hecoinfo.com)
13+
- [testnet.hecoinfo.com](https://testnet.hecoinfo.com)
14+
- [ftmscan.com](https://ftmscan.com)
15+
- [testnet.ftmscan.com](https://testnet.ftmscan.com)
16+
- [optimistic.etherscan.io](https://optimistic.etherscan.io)
17+
- [kovan-optimistic.etherscan.io](https://kovan-optimistic.etherscan.io)
18+
- [polygonscan.com](https://polygonscan.com)
19+
- [testnet.polygonscan.com](https://testnet.polygonscan.com)
20+
- [arbiscan.io](https://arbiscan.io)
21+
- [testnet.arbiscan.io](https://testnet.arbiscan.io)
22+
- [snowtrace.io](https://snowtrace.io)
23+
- [testnet.snowtrace.io](https://testnet.snowtrace.io)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { expect } from "earljs";
2+
3+
import { explorerApiUrls } from "../ethereum-viewer/src/explorer/networks";
4+
import { apiUrlToWebsite } from "../ethereum-viewer/src/explorer/apiUrlToWebsite";
5+
import { ethViewerCommands } from "../vscode-host/src/deth/commands/ethViewerCommands";
6+
import { givenUrl } from "../vscode-host/src/test/test-utils";
7+
import { toDethNet } from "./bookmarklet";
8+
9+
describe(`bookmarklet: ${toDethNet.name}`, () => {
10+
const websiteUrls = Object.values(explorerApiUrls).map(
11+
(url) =>
12+
apiUrlToWebsite(url) +
13+
"/address/0x0000000000000000000000000000000000000000"
14+
);
15+
16+
it("returns hostname ending with deth.net", () => {
17+
const urls = websiteUrls.map(toDethNet);
18+
for (const actual of urls) {
19+
expect(actual).toEqual(expect.stringMatching(/\.deth\.net\//));
20+
}
21+
});
22+
23+
it("preserves contract address", () => {
24+
const urls = websiteUrls.map(toDethNet);
25+
for (const actual of urls) {
26+
expect(actual).toEqual(expect.stringMatching(/address\/0x[0-9a-f]{40}/));
27+
}
28+
29+
const address = `0x${Math.floor(
30+
Math.random() * 10 ** Math.floor(Math.log10(Number.MAX_SAFE_INTEGER))
31+
)}`.padEnd(42, "0");
32+
33+
expect(toDethNet(`https://etherscan.io/address/${address}`)).toEqual(
34+
`https://etherscan.deth.net/address/${address}`
35+
);
36+
});
37+
38+
it("preserves explorer api name for all explorers", () => {
39+
for (const apiName of Object.keys(explorerApiUrls)) {
40+
const websiteUrl =
41+
apiUrlToWebsite(
42+
explorerApiUrls[apiName as keyof typeof explorerApiUrls]
43+
) + "/address/0x0000000000000000000000000000000000000000";
44+
45+
const ecvUrl = toDethNet(websiteUrl);
46+
47+
givenUrl(ecvUrl);
48+
expect(ethViewerCommands.getApiName()).toEqual(apiName);
49+
}
50+
});
51+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* How to turn this into a bookmarklet:
3+
* - Replace `return href.` in function body with `location.href = location.href.`
4+
* - Prefix with `javascript: `
5+
* - Paste it into the readme or test it in your browser
6+
*/
7+
export function toDethNet(href: string): string {
8+
return href.replace(/\.\w+(\/)/, ".deth.net/");
9+
}

packages/bookmarklet/tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true,
4+
"checkJs": true,
5+
"noEmit": true,
6+
"module": "CommonJS"
7+
},
8+
"include": [
9+
"*.ts",
10+
"../ethereum-viewer/src/explorer/**/*.ts",
11+
"../vscode-host/src/deth/**/*.ts",
12+
"../vscode-host/src/test/**/*.ts"
13+
]
14+
}

packages/ethereum-viewer/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"watch": "webpack --watch",
1111
"pretest": "yarn run build",
1212
"lint": "eslint src --ext ts",
13-
"serve": "serve --cors -l 5000 --ssl-cert ../../certs/localhost.pem --ssl-key ../../certs/localhost-key.pem"
13+
"serve": "serve --cors -l 5000 --ssl-cert ../../certs/localhost.pem --ssl-key ../../certs/localhost-key.pem",
14+
"supported-explorers": "ts-node ./scripts/supported-explorers"
1415
},
1516
"dependencies": {
1617
"fast-json-stable-stringify": "^2.1.0",
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { apiUrlToWebsite } from "../src/explorer/apiUrlToWebsite";
2+
import { explorerApiUrls } from "../src/explorer/networks";
3+
4+
function printSupportedExplorers() {
5+
console.log(
6+
"\n" +
7+
Object.values(explorerApiUrls)
8+
.map((url) => {
9+
const websiteUrl = apiUrlToWebsite(url);
10+
const label = websiteUrl.replace(/^https?:\/\//, "");
11+
return `- [${label}](${websiteUrl})`;
12+
})
13+
.join("\n") +
14+
"\n"
15+
);
16+
}
17+
18+
if (require.main === module) printSupportedExplorers();

0 commit comments

Comments
 (0)