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

Commit

Permalink
enable multiple text fragments in one url
Browse files Browse the repository at this point in the history
  • Loading branch information
chunliu committed Jun 26, 2020
1 parent de4c0b6 commit 9b5cee5
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ The extension works in the following way.
## Note

- 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.
- 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.
- 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.
9 changes: 4 additions & 5 deletions code/src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ function copyToClipboard (content) {

function quoteOnClick (info) {
// Create the fragment link.
const directive = '#:~:text=';
// If the page url has a fragment already, remove it first.
let directive = '#:~:text=';
// Support multiple text fragments.
const regex = RegExp(directive, 'g');
let pageUrl = info.pageUrl;
if (regex.test(info.pageUrl)) {
pageUrl = info.pageUrl.split(directive)[0];
directive = '&text=';
}

const fragmentLink = pageUrl + directive + getFragment(info.selectionText);
const fragmentLink = info.pageUrl + directive + getFragment(info.selectionText);

switch (info.menuItemId) {
case 'sttf_open': {
Expand Down
4 changes: 2 additions & 2 deletions code/src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"manifest_version": 2,
"name": "STTF Url Generator",
"version": "1.0",
"version": "1.1",
"author": "Chun Liu",
"description": "Generate a scroll-to-text-fragment url and open it in a new tab.",
"description": "Generate a url with text fragment and copy it to the clipboard or open it in a new tab.",
"permissions": [
"clipboardWrite",
"contextMenus",
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ The extension works in the following way.
## Note

- 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.
- 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.
- 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 comments on commit 9b5cee5

Please sign in to comment.