|
5 | 5 | const { CommonDialog } = ChromeUtils.importESModule(
|
6 | 6 | "resource://gre/modules/CommonDialog.sys.mjs"
|
7 | 7 | );
|
| 8 | +const { XPCOMUtils } = ChromeUtils.importESModule( |
| 9 | + "resource://gre/modules/XPCOMUtils.sys.mjs" |
| 10 | +); |
| 11 | + |
| 12 | +const lazy = {}; |
| 13 | + |
| 14 | +XPCOMUtils.defineLazyServiceGetter( |
| 15 | + lazy, |
| 16 | + "gContentAnalysis", |
| 17 | + "@mozilla.org/contentanalysis;1", |
| 18 | + Ci.nsIContentAnalysis |
| 19 | +); |
8 | 20 |
|
9 | 21 | // imported by adjustableTitle.js loaded in the same context:
|
10 | 22 | /* globals PromptUtils */
|
@@ -123,6 +135,58 @@ function commonDialogOnLoad() {
|
123 | 135 | // If the icon hasn't loaded yet, size the window to the content again when
|
124 | 136 | // it does, as its layout can change.
|
125 | 137 | ui.infoIcon.addEventListener("load", () => window.sizeToContent());
|
| 138 | + if (lazy.gContentAnalysis.isActive && args.owningBrowsingContext?.isContent) { |
| 139 | + ui.loginTextbox?.addEventListener("paste", async event => { |
| 140 | + let data = event.clipboardData.getData("text/plain"); |
| 141 | + if (data?.length > 0) { |
| 142 | + // Prevent the paste from happening until content analysis returns a response |
| 143 | + event.preventDefault(); |
| 144 | + // Selections can be forward or backward, so use min/max |
| 145 | + const startIndex = Math.min( |
| 146 | + ui.loginTextbox.selectionStart, |
| 147 | + ui.loginTextbox.selectionEnd |
| 148 | + ); |
| 149 | + const endIndex = Math.max( |
| 150 | + ui.loginTextbox.selectionStart, |
| 151 | + ui.loginTextbox.selectionEnd |
| 152 | + ); |
| 153 | + const selectionDirection = |
| 154 | + endIndex < startIndex ? "backward" : "forward"; |
| 155 | + try { |
| 156 | + const response = await lazy.gContentAnalysis.analyzeContentRequest( |
| 157 | + { |
| 158 | + requestToken: Services.uuid.generateUUID().toString(), |
| 159 | + resources: [], |
| 160 | + analysisType: Ci.nsIContentAnalysis.eBulkDataEntry, |
| 161 | + operationTypeForDisplay: Ci.nsIContentAnalysisRequest.eClipboard, |
| 162 | + url: args.owningBrowsingContext.currentURI.spec, |
| 163 | + textContent: data, |
| 164 | + windowGlobalParent: |
| 165 | + args.owningBrowsingContext.currentWindowContext, |
| 166 | + }, |
| 167 | + true |
| 168 | + ); |
| 169 | + if (response.shouldAllowContent) { |
| 170 | + ui.loginTextbox.value = |
| 171 | + ui.loginTextbox.value.slice(0, startIndex) + |
| 172 | + data + |
| 173 | + ui.loginTextbox.value.slice(endIndex); |
| 174 | + ui.loginTextbox.focus(); |
| 175 | + if (startIndex !== endIndex) { |
| 176 | + // Select the pasted text |
| 177 | + ui.loginTextbox.setSelectionRange( |
| 178 | + startIndex, |
| 179 | + startIndex + data.length, |
| 180 | + selectionDirection |
| 181 | + ); |
| 182 | + } |
| 183 | + } |
| 184 | + } catch (error) { |
| 185 | + console.error("Content analysis request returned error: ", error); |
| 186 | + } |
| 187 | + } |
| 188 | + }); |
| 189 | + } |
126 | 190 |
|
127 | 191 | window.getAttention();
|
128 | 192 | }
|
|
0 commit comments