|
24 | 24 |
|
25 | 25 | const createTextFragment = () => {
|
26 | 26 | const selection = window.getSelection();
|
27 |
| - // eslint-disable-next-line no-undef |
| 27 | + |
28 | 28 | const result = exports.generateFragment(selection);
|
29 | 29 | let url = `${location.origin}${location.pathname}${location.search}${
|
30 | 30 | location.hash ? location.hash : '#'
|
31 | 31 | }`;
|
32 | 32 | if (result.status === 0) {
|
33 | 33 | const fragment = result.fragment;
|
34 |
| - const prefix = fragment.prefix ? |
35 |
| - `${encodeURIComponent(fragment.prefix)}-,` : |
36 |
| - ''; |
37 |
| - const suffix = fragment.suffix ? |
38 |
| - `,-${encodeURIComponent(fragment.suffix)}` : |
39 |
| - ''; |
| 34 | + const prefix = fragment.prefix |
| 35 | + ? `${encodeURIComponent(fragment.prefix)}-,` |
| 36 | + : ''; |
| 37 | + const suffix = fragment.suffix |
| 38 | + ? `,-${encodeURIComponent(fragment.suffix)}` |
| 39 | + : ''; |
40 | 40 | const textStart = encodeURIComponent(fragment.textStart);
|
41 |
| - const textEnd = fragment.textEnd ? |
42 |
| - `,${encodeURIComponent(fragment.textEnd)}` : |
43 |
| - ''; |
| 41 | + const textEnd = fragment.textEnd |
| 42 | + ? `,${encodeURIComponent(fragment.textEnd)}` |
| 43 | + : ''; |
44 | 44 | url = `${url}:~:text=${prefix}${textStart}${textEnd}${suffix}`;
|
45 | 45 | copyToClipboard(url, selection);
|
46 | 46 | reportSuccess();
|
|
72 | 72 | const reportFailure = (status) => {
|
73 | 73 | const statusCodes = {
|
74 | 74 | 1: 'INVALID_SELECTION: The selection provided could not be used.',
|
75 |
| - // eslint-disable-next-line max-len |
| 75 | + |
76 | 76 | 2: 'AMBIGUOUS: No unique fragment could be identified for this selection.',
|
77 | 77 | 3: 'TIMEOUT: Computation could not complete in time.',
|
78 | 78 | 4: 'EXECUTION_FAILED: Unknown error.',
|
79 | 79 | };
|
80 | 80 |
|
81 | 81 | window.queueMicrotask(() => {
|
82 | 82 | alert(
|
83 |
| - `🛑 ${browser.i18n.getMessage( |
84 |
| - 'extension_name', |
85 |
| - )}:\n${browser.i18n.getMessage('link_failure')}\n\n(${ |
86 |
| - statusCodes[status] |
87 |
| - })`, |
| 83 | + `🛑 ${browser.i18n.getMessage( |
| 84 | + 'extension_name' |
| 85 | + )}:\n${browser.i18n.getMessage('link_failure')}\n\n(${ |
| 86 | + statusCodes[status] |
| 87 | + })` |
88 | 88 | );
|
89 | 89 | });
|
90 | 90 | return true;
|
91 | 91 | };
|
92 | 92 |
|
93 | 93 | const copyToClipboard = (url, selection) => {
|
94 | 94 | browser.storage.sync.get(
|
95 |
| - { |
96 |
| - linkStyle: 'rich', |
97 |
| - linkText: browser.i18n.getMessage('link_text_option_1'), |
98 |
| - }, |
99 |
| - async (items) => { |
100 |
| - const linkStyle = items.linkStyle; |
101 |
| - // Send message to offscreen document |
102 |
| - const selectedText = selection.toString(); |
103 |
| - const linkText = items.linkText; |
104 |
| - let html = ''; |
105 |
| - if (selection.rangeCount) { |
106 |
| - const container = document.createElement('div'); |
107 |
| - for (let i = 0, len = selection.rangeCount; i < len; ++i) { |
| 95 | + { |
| 96 | + linkStyle: 'rich', |
| 97 | + linkText: browser.i18n.getMessage('link_text_option_1'), |
| 98 | + }, |
| 99 | + async (items) => { |
| 100 | + const linkStyle = items.linkStyle; |
| 101 | + // Send message to offscreen document |
| 102 | + const selectedText = selection.toString(); |
| 103 | + const linkText = items.linkText; |
| 104 | + let html = ''; |
| 105 | + if (selection.rangeCount) { |
| 106 | + const container = document.createElement('div'); |
| 107 | + for (let i = 0, len = selection.rangeCount; i < len; ++i) { |
108 | 108 | // prettier-ignore
|
109 |
| - container.appendChild( |
| 109 | + container.appendChild( |
110 | 110 | selection.getRangeAt(i).cloneContents());
|
| 111 | + } |
| 112 | + html = container.innerHTML; |
| 113 | + } |
| 114 | + browser.runtime.sendMessage( |
| 115 | + { |
| 116 | + target: 'offscreen', |
| 117 | + data: { linkStyle, url, selectedText, html, linkText }, |
| 118 | + }, |
| 119 | + (response) => { |
| 120 | + if (response) { |
| 121 | + log('🎉', url); |
111 | 122 | }
|
112 |
| - html = container.innerHTML; |
113 | 123 | }
|
114 |
| - browser.runtime.sendMessage( |
115 |
| - { |
116 |
| - target: 'offscreen', |
117 |
| - data: {linkStyle, url, selectedText, html, linkText}, |
118 |
| - }, |
119 |
| - (response) => { |
120 |
| - if (response) { |
121 |
| - log('🎉', url); |
122 |
| - } |
123 |
| - }, |
124 |
| - ); |
125 |
| - }, |
| 124 | + ); |
| 125 | + } |
126 | 126 | );
|
127 | 127 | };
|
128 | 128 |
|
|
136 | 136 | return sendResponse('pong');
|
137 | 137 | }
|
138 | 138 | });
|
139 |
| - // eslint-disable-next-line no-undef |
140 | 139 | })(chrome || browser);
|
0 commit comments