Quellcode durchsuchen

request "identity" permission in the options page
fix #1002

Gildas vor 3 Jahren
Ursprung
Commit
65edb09102
3 geänderte Dateien mit 22 neuen und 14 gelöschten Zeilen
  1. 0 3
      src/core/bg/downloads.js
  2. 0 11
      src/lib/gdrive/gdrive.js
  3. 22 0
      src/ui/bg/ui-options.js

+ 0 - 3
src/core/bg/downloads.js

@@ -44,8 +44,6 @@ const CONFLICT_ACTION_SKIP = "skip";
 const CONFLICT_ACTION_UNIQUIFY = "uniquify";
 const REGEXP_ESCAPE = /([{}()^$&.*?/+|[\\\\]|\]|-)/g;
 
-const manifest = browser.runtime.getManifest();
-const requestPermissionIdentity = manifest.optional_permissions && manifest.optional_permissions.includes("identity");
 const gDrive = new GDrive(GDRIVE_CLIENT_ID, GDRIVE_CLIENT_KEY, SCOPES);
 export {
 	onMessage,
@@ -182,7 +180,6 @@ async function getAuthInfo(authOptions, force) {
 	const options = {
 		interactive: true,
 		forceWebAuthFlow: authOptions.forceWebAuthFlow,
-		requestPermissionIdentity,
 		launchWebAuthFlow: options => launchWebAuthFlow(options),
 		extractAuthCode: authURL => extractAuthCode(authURL)
 	};

+ 0 - 11
src/lib/gdrive/gdrive.js

@@ -29,8 +29,6 @@ const REVOKE_ACCESS_URL = "https://accounts.google.com/o/oauth2/revoke";
 const GDRIVE_URL = "https://www.googleapis.com/drive/v3/files";
 const GDRIVE_UPLOAD_URL = "https://www.googleapis.com/upload/drive/v3/files";
 
-let requestPermissionIdentityNeeded = true;
-
 class GDrive {
 	constructor(clientId, clientKey, scopes) {
 		this.clientId = clientId;
@@ -40,15 +38,6 @@ class GDrive {
 		setInterval(() => this.folderIds.clear(), 60 * 1000);
 	}
 	async auth(options = { interactive: true }) {
-		if (options.requestPermissionIdentity && requestPermissionIdentityNeeded) {
-			try {
-				await browser.permissions.request({ permissions: ["identity"] });
-				requestPermissionIdentityNeeded = false;
-			}
-			catch (error) {
-				// ignored;
-			}
-		}
 		if (nativeAuth(options)) {
 			this.accessToken = await browser.identity.getAuthToken({ interactive: options.interactive });
 			return { revokableAccessToken: this.accessToken };

+ 22 - 0
src/ui/bg/ui-options.js

@@ -262,6 +262,8 @@ const cancelButton = document.getElementById("cancelButton");
 const promptInput = document.getElementById("promptInput");
 const promptCancelButton = document.getElementById("promptCancelButton");
 const promptConfirmButton = document.getElementById("promptConfirmButton");
+const manifest = browser.runtime.getManifest();
+const requestPermissionIdentity = manifest.optional_permissions && manifest.optional_permissions.includes("identity");
 
 let sidePanelDisplay;
 if (location.href.endsWith("#side-panel")) {
@@ -454,6 +456,7 @@ passReferrerOnErrorInput.addEventListener("click", passReferrerOnError, false);
 autoSaveExternalSaveInput.addEventListener("click", () => enableExternalSave(autoSaveExternalSaveInput), false);
 saveWithCompanionInput.addEventListener("click", () => enableExternalSave(saveWithCompanionInput), false);
 saveToClipboardInput.addEventListener("click", onClickSaveToClipboard, false);
+saveToGDriveInput.addEventListener("click", onClickSaveToGDrive, false);
 addProofInput.addEventListener("click", async event => {
 	if (addProofInput.checked) {
 		addProofInput.checked = false;
@@ -1023,6 +1026,25 @@ async function onClickSaveToClipboard() {
 	await refresh();
 }
 
+async function onClickSaveToGDrive() {
+	if (saveToGDriveInput.checked) {
+		saveToGDriveInput.checked = false;
+		try {
+			if (requestPermissionIdentity) {
+				const permissionGranted = await browser.permissions.request({ permissions: ["identity"] });
+				if (permissionGranted) {
+					saveToGDriveInput.checked = true;
+				}
+			}
+		} catch (error) {
+			saveToGDriveInput.checked = false;
+			await browser.runtime.sendMessage({ method: "downloads.disableGDrive" });
+		}
+	}
+	await update();
+	await refresh();
+}
+
 async function disableDestinationPermissions(permissions, disableGDrive = true) {
 	if (disableGDrive) {
 		await browser.runtime.sendMessage({ method: "downloads.disableGDrive" });