Skip to content

Commit

Permalink
feat: use optional permission for clipboard access
Browse files Browse the repository at this point in the history
  • Loading branch information
dessant committed Oct 21, 2023
1 parent 6bb518c commit f805a49
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 23 deletions.
32 changes: 23 additions & 9 deletions src/action/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,9 @@ import {
canShare,
sendLargeMessage,
handleBrowserActionEscapeKey,
getAppTheme
getAppTheme,
hasClipboardReadPermission,
requestClipboardReadPermission
} from 'utils/app';
import {getText, getActiveTab, isValidTab} from 'utils/common';
import {enableContributions} from 'utils/config';
Expand Down Expand Up @@ -512,9 +514,15 @@ export default {
onPasteButtonClick: function () {
if (!this.startProcessing()) return;
this.processClipboardImages({showError: true}).finally(() => {
this.stopProcessing();
});
requestClipboardReadPermission()
.then(granted =>
granted
? this.processClipboardImages({showError: true})
: showNotification({messageId: 'error_noClipboardReadAccess'})
)
.finally(() => {
this.stopProcessing();
});
},
onEngineClick: function (engine) {
Expand Down Expand Up @@ -866,7 +874,8 @@ export default {
options.autoPasteAction &&
!this.$env.isSafari &&
!this.$env.isSamsung &&
!(this.$env.isMobile && this.$env.isFirefox);
!(this.$env.isMobile && this.$env.isFirefox) &&
(await hasClipboardReadPermission());
this.setupPinnedButtons({maxPins: this.maxPinnedToolbarButtons});
Expand Down Expand Up @@ -1006,8 +1015,11 @@ body {
max-height: 100px;
padding-top: 8px;
padding-bottom: 24px;
transition: max-height 0.3s ease, padding-top 0.3s ease,
padding-bottom 0.3s ease, opacity 0.2s ease;
transition:
max-height 0.3s ease,
padding-top 0.3s ease,
padding-bottom 0.3s ease,
opacity 0.2s ease;
}
.settings-enter-from,
Expand Down Expand Up @@ -1073,8 +1085,10 @@ body {
object-fit: scale-down;
border-radius: 8px;
box-shadow: 0px 3px 5px -1px rgba(0, 0, 0, 0.06),
0px 6px 10px 0px rgba(0, 0, 0, 0.04), 0px 1px 12px 0px rgba(0, 0, 0, 0.03);
box-shadow:
0px 3px 5px -1px rgba(0, 0, 0, 0.06),
0px 6px 10px 0px rgba(0, 0, 0, 0.04),
0px 1px 12px 0px rgba(0, 0, 0, 0.03);
}
& .preview-close-button {
Expand Down
10 changes: 10 additions & 0 deletions src/assets/locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,16 @@
"description": "Error message."
},

"error_noClipboardReadAccess": {
"message": "Cannot get data from the clipboard.",
"description": "Error message."
},

"error_noClipboardWriteAccess": {
"message": "Cannot add data to the clipboard.",
"description": "Error message."
},

"error_pageParseError": {
"message": "Something went wrong. The web page could not be parsed.",
"description": "Error message."
Expand Down
3 changes: 2 additions & 1 deletion src/assets/manifest/chrome.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

"permissions": [
"alarms",
"clipboardRead",
"contextMenus",
"storage",
"unlimitedStorage",
Expand All @@ -23,6 +22,8 @@
"<all_urls>"
],

"optional_permissions": ["clipboardRead"],

"content_security_policy": "default-src 'self'; style-src 'self' 'unsafe-inline'; img-src * data: blob:; connect-src * blob:; object-src 'none';",

"icons": {
Expand Down
3 changes: 2 additions & 1 deletion src/assets/manifest/edge.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

"permissions": [
"alarms",
"clipboardRead",
"contextMenus",
"storage",
"unlimitedStorage",
Expand All @@ -23,6 +22,8 @@
"<all_urls>"
],

"optional_permissions": ["clipboardRead"],

"content_security_policy": "default-src 'self'; style-src 'self' 'unsafe-inline'; img-src * data: blob:; connect-src * blob:; object-src 'none';",

"icons": {
Expand Down
3 changes: 2 additions & 1 deletion src/assets/manifest/firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

"permissions": [
"alarms",
"clipboardRead",
"contextMenus",
"storage",
"unlimitedStorage",
Expand All @@ -31,6 +30,8 @@
"<all_urls>"
],

"optional_permissions": ["clipboardRead"],

"content_security_policy": "default-src 'self'; style-src 'self' 'unsafe-inline'; img-src * data: blob:; connect-src * blob:; object-src 'none';",

"icons": {
Expand Down
3 changes: 2 additions & 1 deletion src/assets/manifest/opera.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

"permissions": [
"alarms",
"clipboardRead",
"contextMenus",
"storage",
"unlimitedStorage",
Expand All @@ -23,6 +22,8 @@
"<all_urls>"
],

"optional_permissions": ["clipboardRead"],

"content_security_policy": "default-src 'self'; style-src 'self' 'unsafe-inline'; img-src * data: blob:; connect-src * blob:; object-src 'none';",

"icons": {
Expand Down
24 changes: 17 additions & 7 deletions src/browse/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ import {
getEngineIcon,
validateShareId,
sendLargeMessage,
getAppTheme
getAppTheme,
requestClipboardReadPermission
} from 'utils/app';
import {getText} from 'utils/common';
import {optionKeys} from 'utils/data';
Expand Down Expand Up @@ -409,9 +410,15 @@ export default {
onPasteButtonClick: function () {
if (!this.startProcessing()) return;
this.processClipboardImages().finally(() => {
this.stopProcessing();
});
requestClipboardReadPermission()
.then(granted =>
granted
? this.processClipboardImages()
: showNotification({messageId: 'error_noClipboardReadAccess'})
)
.finally(() => {
this.stopProcessing();
});
},
onFileEvent: function (ev, source) {
Expand Down Expand Up @@ -694,7 +701,8 @@ export default {
}
border-radius: 8px;
box-shadow: 0px 3px 5px -1px rgba(0, 0, 0, 0.06),
box-shadow:
0px 3px 5px -1px rgba(0, 0, 0, 0.06),
0px 6px 10px 0px rgba(0, 0, 0, 0.04),
0px 1px 12px 0px rgba(0, 0, 0, 0.03);
}
Expand Down Expand Up @@ -748,8 +756,10 @@ export default {
max-height: 96px;
border-radius: 8px;
box-shadow: 0px 3px 5px -1px rgba(0, 0, 0, 0.12),
0px 6px 10px 0px rgba(0, 0, 0, 0.08), 0px 1px 12px 0px rgba(0, 0, 0, 0.06);
box-shadow:
0px 3px 5px -1px rgba(0, 0, 0, 0.12),
0px 6px 10px 0px rgba(0, 0, 0, 0.08),
0px 1px 12px 0px rgba(0, 0, 0, 0.06);
}
& .list-items {
Expand Down
10 changes: 8 additions & 2 deletions src/options/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,12 @@ import {includes, without} from 'lodash-es';
import draggable from 'vuedraggable';
import storage from 'storage/storage';
import {getListItems, canShare, showContributePage} from 'utils/app';
import {
getListItems,
canShare,
showContributePage,
hasClipboardReadPermission
} from 'utils/app';
import {getText} from 'utils/common';
import {enableContributions} from 'utils/config';
import {optionKeys} from 'utils/data';
Expand Down Expand Up @@ -347,7 +352,8 @@ export default {
this.autoPasteEnabled =
!this.$env.isSafari &&
!this.$env.isSamsung &&
!(this.$env.isMobile && this.$env.isFirefox);
!(this.$env.isMobile && this.$env.isFirefox) &&
(await hasClipboardReadPermission());
this.dataLoaded = true;
},
Expand Down
17 changes: 16 additions & 1 deletion src/utils/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1854,6 +1854,19 @@ function addThemeListener(callback) {
addAppThemeListener(callback);
}

async function hasClipboardReadPermission() {
return browser.permissions.contains({permissions: ['clipboardRead']});
}

async function requestClipboardReadPermission() {
// Safari: requesting clipboard access is handled by the browser
if (targetEnv === 'safari') {
return true;
}

return browser.permissions.request({permissions: ['clipboardRead']});
}

export {
getEnabledEngines,
getSupportedEngines,
Expand Down Expand Up @@ -1935,5 +1948,7 @@ export {
processAppUse,
addSystemThemeListener,
addAppThemeListener,
addThemeListener
addThemeListener,
hasClipboardReadPermission,
requestClipboardReadPermission
};

0 comments on commit f805a49

Please sign in to comment.