From c593c54228b045439135cd303134b3ab4737f3ad Mon Sep 17 00:00:00 2001 From: Gusted Date: Thu, 3 Jun 2021 15:42:52 +0200 Subject: [PATCH] Send styleInfo We will be adding the feature to add style based of the currentStyle, see paring commit https://github.com/userstyles-world/userstyles.world/commit/31813da30088eb9438299808ac3232f1e2c9fd5d --- background/style-manager.js | 2 +- background/token-manager.js | 16 +++++++++++----- edit/usw-linking.js | 3 ++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/background/style-manager.js b/background/style-manager.js index 28ddb649..d9279ff3 100644 --- a/background/style-manager.js +++ b/background/style-manager.js @@ -361,7 +361,7 @@ const styleMan = (() => { switch (reason) { case 'link': style._usw = { - token: await tokenMan.getToken('userstylesworld', true, style.id), + token: await tokenMan.getToken('userstylesworld', true, style), }; for (const [k, v] of Object.entries(await retrieveStyleInformation(style._usw.token))) { style._usw[k] = v; diff --git a/background/token-manager.js b/background/token-manager.js index 974e1af1..d2dba0cf 100644 --- a/background/token-manager.js +++ b/background/token-manager.js @@ -52,7 +52,6 @@ const tokenMan = (() => { flow: 'code', clientId: 'zeDmKhJIfJqULtcrGMsWaxRtWHEimKgS', clientSecret: 'wqHsvTuThQmXmDiVvOpZxPwSIbyycNFImpAOTxjaIRqDbsXcTOqrymMJKsOMuibFaij' + - 'ZZAkVYTDbLkQuYFKqgpMsMlFlgwQOYHvHFbgxQHDTwwdOroYhOwFuekCwXUlk', authURL: URLS.usw + 'api/oauth/authorize_style', tokenURL: URLS.usw + 'api/oauth/access_token', redirect_uri: 'https://gusted.xyz/callback_helper/', @@ -78,8 +77,8 @@ const tokenMan = (() => { return AUTH[name].clientId; }, - async getToken(name, interactive, styleId) { - const k = tokenMan.buildKeys(name, styleId); + async getToken(name, interactive, style) { + const k = tokenMan.buildKeys(name, style.id); const obj = await chromeLocal.get(k.LIST); if (obj[k.TOKEN]) { if (!obj[k.EXPIRE] || Date.now() < obj[k.EXPIRE]) { @@ -92,7 +91,11 @@ const tokenMan = (() => { if (!interactive) { throw new Error(`Invalid token: ${name}`); } - return authUser(name, k, interactive); + const styleInformation = name === 'userstylesworld' ? JSON.stringify({ + 'code': style.sourceCode, + 'name': style.name, + }) : null; + return authUser(name, k, interactive, styleInformation); }, async revokeToken(name, styleId) { @@ -132,7 +135,7 @@ const tokenMan = (() => { return handleTokenResult(result, k); } - async function authUser(name, k, interactive = false) { + async function authUser(name, k, interactive = false, styleInformation = null) { await require(['/vendor/webext-launch-web-auth-flow/webext-launch-web-auth-flow.min']); /* global webextLaunchWebAuthFlow */ const provider = AUTH[name]; @@ -143,6 +146,9 @@ const tokenMan = (() => { redirect_uri: provider.redirect_uri || chrome.identity.getRedirectURL(), state, }; + if (styleInformation) { + query['styleInfo'] = styleInformation; + } if (provider.scopes) { query.scope = provider.scopes.join(' '); } diff --git a/edit/usw-linking.js b/edit/usw-linking.js index 7d14b660..54d58e4d 100644 --- a/edit/usw-linking.js +++ b/edit/usw-linking.js @@ -18,7 +18,8 @@ function connectToPort() { function linkToUSW() { connectToPort(); - uswPort.postMessage({reason: 'link', data: editor.style}); + const data = Object.assign(editor.style, {sourceCode: editor.getEditors()[0].getValue()}); + uswPort.postMessage({reason: 'link', data}); } /* exported revokeLinking */