Skip to content

Commit 87b3cff

Browse files
authored
Merge pull request #355 from mkobayashime/document-title
Refactor `copyDocumentTitle` using `document.title`
2 parents a819fe4 + d094824 commit 87b3cff

3 files changed

Lines changed: 10 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Copy name and normalized link of the current item to clipboard
1414

1515
### [Copy document title](https://raw.githubusercontent.com/mkobayashime/bookmarklets/main/dist/copyDocumentTitle.js)
1616

17-
Copy the value of `<title>` element to clipboard
17+
Copy title of the document to clipboard
1818

1919
### [Enable selection](https://raw.githubusercontent.com/mkobayashime/bookmarklets/main/dist/enableSelection.js)
2020

dist/copyDocumentTitle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/copyDocumentTitle.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
/**
22
* @title Copy document title
3-
* @description Copy the value of `<title>` element to clipboard
3+
* @description Copy title of the document to clipboard
44
*/
55

66
import { copyToClipboard } from "./utils/copyToClipboard";
77

8-
const titleElement = document.querySelector("title");
9-
const title = titleElement?.innerText.trim().split("\n").join("");
10-
(async () => {
11-
if (title) await copyToClipboard(title, false);
12-
})().catch((err) => console.error(err));
8+
void (async () => {
9+
if (document.title) {
10+
await copyToClipboard(document.title, false);
11+
} else {
12+
window.alert("Failed to get document title");
13+
}
14+
})();

0 commit comments

Comments
 (0)