Skip to content
This repository was archived by the owner on Aug 16, 2022. It is now read-only.

Commit 9b5cee5

Browse files
committed
enable multiple text fragments in one url
1 parent de4c0b6 commit 9b5cee5

File tree

4 files changed

+8
-9
lines changed

4 files changed

+8
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ The extension works in the following way.
2323
## Note
2424

2525
- According to the [spec](https://wicg.github.io/scroll-to-text-fragment/#word-boundaries#:~:text=The%20substring%20%22mountain%20range%22%20is%20word%20bounded%20within%20the%20string%20%22An%20impressive%20mountain%20range%22%20but%20not%20within%20%22An%20impressive%20mountain%20ranger%22.), the selection of the text snippet must adhere to the word boundaries. In another words, the selection needs to start from the beginning of the first word and end at the ending of the last word of the text snippet. If the selection starts or ends at a character in a word, the browser would not be able to find the matching accurately.
26-
- Due to the [algorithm](https://wicg.github.io/scroll-to-text-fragment/#finding-ranges-in-a-document#:~:text=each%20of%20prefix%2C%20textStart%2C%20textEnd%2C%20and%20suffix%20will%20only%20match%20text%20within%20a%20single%20block.) of finding ranges in a document, the selection of the text snippet should avoid crossing multiple blocks. For example, if a selection is across a `<h2>` and a `<div>`, the browser may not be able to scroll to the text fragment accurately.
26+
- Due to the [algorithm](https://wicg.github.io/scroll-to-text-fragment/#finding-ranges-in-a-document#:~:text=each%20of%20prefix%2C%20textStart%2C%20textEnd%2C%20and%20suffix%20will%20only%20match%20text%20within%20a%20single%20block.) of finding ranges in a document, the selection of the text snippet should avoid crossing multiple ["block-level" elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements#Elements#:~:text=%22block-level%22%20elements). For example, if a selection is across a `<h2>` and a `<div>`, the browser may not be able to scroll to the text fragment accurately.

code/src/background.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,14 @@ function copyToClipboard (content) {
3030

3131
function quoteOnClick (info) {
3232
// Create the fragment link.
33-
const directive = '#:~:text=';
34-
// If the page url has a fragment already, remove it first.
33+
let directive = '#:~:text=';
34+
// Support multiple text fragments.
3535
const regex = RegExp(directive, 'g');
36-
let pageUrl = info.pageUrl;
3736
if (regex.test(info.pageUrl)) {
38-
pageUrl = info.pageUrl.split(directive)[0];
37+
directive = '&text=';
3938
}
4039

41-
const fragmentLink = pageUrl + directive + getFragment(info.selectionText);
40+
const fragmentLink = info.pageUrl + directive + getFragment(info.selectionText);
4241

4342
switch (info.menuItemId) {
4443
case 'sttf_open': {

code/src/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"manifest_version": 2,
33
"name": "STTF Url Generator",
4-
"version": "1.0",
4+
"version": "1.1",
55
"author": "Chun Liu",
6-
"description": "Generate a scroll-to-text-fragment url and open it in a new tab.",
6+
"description": "Generate a url with text fragment and copy it to the clipboard or open it in a new tab.",
77
"permissions": [
88
"clipboardWrite",
99
"contextMenus",

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ The extension works in the following way.
3030
## Note
3131

3232
- According to the [spec](https://wicg.github.io/scroll-to-text-fragment/#word-boundaries#:~:text=The%20substring%20%22mountain%20range%22%20is%20word%20bounded%20within%20the%20string%20%22An%20impressive%20mountain%20range%22%20but%20not%20within%20%22An%20impressive%20mountain%20ranger%22.), the selection of the text snippet must adhere to the word boundaries. In another words, the selection needs to start from the beginning of the first word and end at the ending of the last word of the text snippet. If the selection starts or ends at a character in a word, the browser would not be able to find the matching accurately.
33-
- Due to the [algorithm](https://wicg.github.io/scroll-to-text-fragment/#finding-ranges-in-a-document#:~:text=each%20of%20prefix%2C%20textStart%2C%20textEnd%2C%20and%20suffix%20will%20only%20match%20text%20within%20a%20single%20block.) of finding ranges in a document, the selection of the text snippet should avoid crossing multiple blocks. For example, if a selection is across a `<h2>` and a `<div>`, the browser may not be able to scroll to the text fragment accurately.
33+
- Due to the [algorithm](https://wicg.github.io/scroll-to-text-fragment/#finding-ranges-in-a-document#:~:text=each%20of%20prefix%2C%20textStart%2C%20textEnd%2C%20and%20suffix%20will%20only%20match%20text%20within%20a%20single%20block.) of finding ranges in a document, the selection of the text snippet should avoid crossing multiple ["block-level" elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements#Elements#:~:text=%22block-level%22%20elements). For example, if a selection is across a `<h2>` and a `<div>`, the browser may not be able to scroll to the text fragment accurately.

0 commit comments

Comments
 (0)