From 2f78cca73dacae78208b4f6b022eaa0385c9f9fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jostein=20Kj=C3=B8nigsen?= Date: Thu, 21 Apr 2022 21:21:19 +0200 Subject: [PATCH 1/4] Remove broken functions. - api.get.loggedin_accounts() no longer works. - this also breaks api.get.delegated_to_email() - this also breaks api.get.manager_email() - removing those also mean we no longer need api.helper.get.is_delegated_inbox() either. Bye bye :) --- README.md | 26 -------------------- src/gmail.d.ts | 18 -------------- src/gmail.js | 65 -------------------------------------------------- 3 files changed, 109 deletions(-) diff --git a/README.md b/README.md index 0177ab6e..8505895b 100755 --- a/README.md +++ b/README.md @@ -85,7 +85,6 @@ import "gmail-js"; - [gmail.get**.user_email()**](#gmailgetuser_email) -- [gmail.get**.manager_email()**](#gmailgetmanager_email) - [gmail.get**.current_page()**](#gmailgetcurrent_page) - [gmail.get**.new.email_id()**](#gmailnewgetemail_id) @@ -108,7 +107,6 @@ import "gmail-js"; - [gmail.get**.unread_social_emails()**](#gmailgetunread_emails) - [gmail.get**.last_active()**](#gmailgetlast_active) - [gmail.get**.storage_info()**](#gmailgetstorage_info) -- [gmail.get**.loggedin_accounts()**](#gmailgetloggedin_accounts) - [gmail.get**.beta()**](#gmailgetbeta) - [gmail.get**.localization()**](#gmailgetlocalization) @@ -466,22 +464,6 @@ Returns the current user's email address "california@gmail.com" ``` -#### gmail.get.manager_email() - -Returns the email address of the user currently managing the account (if the inbox is used by the owner, this function returns the same value as [gmail.get**.user_email()**](#gmailgetuser_email)) - -```js -"california@gmail.com" -``` - -#### gmail.get.delegated_to_email() - -Returns the email address of the user the account is currently delegated to (if the inbox is used by the owner, this function returns null) - -```js -"california@gmail.com" -``` - #### gmail.get.storage_info() Returns current user's file storage stats @@ -573,14 +555,6 @@ Gets user's account activity data } ``` -#### gmail.get.loggedin_accounts() - -Returns a list of signed-in accounts (multiple user accounts setup in gmail) - -```json -[{"name":"California","email":"california@gmail.com"}] -``` - #### gmail.get.beta() Although hand picked, this method returns the checks on beta features and deployments diff --git a/src/gmail.d.ts b/src/gmail.d.ts index 5d551ff7..b88d4ee1 100644 --- a/src/gmail.d.ts +++ b/src/gmail.d.ts @@ -124,28 +124,10 @@ interface GmailGet { */ last_active(): GmailLastActive; - /** - Returns a list of signed-in accounts (multiple user accounts - setup in gmail) - */ - loggedin_accounts(): GmailLoggedInAccount[]; - /** Returns the current user's email address */ user_email(): string; - /** - Returns the email address of the user currently managing the - account (if the inbox is used by the owner, this function - returns the same value as gmail.get.user_email()) - */ - manager_email(): string; - /** - Returns the email address of the user the account is currently - delegated to (if the inbox is used by the owner, this function - returns null) - */ - delegated_to_email(): string; /** Returns the Gmail localization, e.g. 'US'. */ diff --git a/src/gmail.js b/src/gmail.js index 426b0f33..ab33388d 100644 --- a/src/gmail.js +++ b/src/gmail.js @@ -96,71 +96,11 @@ var Gmail = function(localJQuery) { }; - api.get.loggedin_accounts = function() { - var i, j, data; - var users = []; - - var globals17 = api.tracker.globals[17]; - for (i in globals17) { - // at least for the delegated inboxes, the index of the mla is not stable - // it was observed to be somewhere between 22 and 24, but we should not depend on it - data = globals17[i]; - - if (data[0] === "mla") { - for(j in data[1]) { - users.push({ - name : data[1][j][4], - email : data[1][j][0], - index: data[1][j][3] - }); - } - - return users; - } - } - - return users; - }; - - api.get.user_email = function() { return api.tracker.globals[10]; }; - api.get.manager_email = function() { - if (api.helper.get.is_delegated_inbox()) { - return api.get.delegated_to_email(); - } - - return api.get.user_email(); - }; - - - api.get.delegated_to_email = function() { - if (!api.helper.get.is_delegated_inbox()) { - return null; - } - - var i, account; - var userIndexPrefix = "/u/"; - var pathname = window.location.pathname; - var delegatedToUserIndex = parseInt(pathname.substring(pathname.indexOf(userIndexPrefix) + userIndexPrefix.length), 10); - - var loggedInAccounts = api.get.loggedin_accounts(); - if (loggedInAccounts && loggedInAccounts.length > 0) { - for (i in loggedInAccounts) { - account = loggedInAccounts[i]; - if (account.index === delegatedToUserIndex) { - return account.email; - } - } - } - - // as a last resort, we query the DOM of the upper right account selection menu - return $(".gb_rb[href$='" + userIndexPrefix + delegatedToUserIndex + "'] .gb_yb").text().split(" ")[0]; - }; - api.helper.get.is_locale = function(locale) { // A locale is a string that begins with 2 letters, either lowercase or uppercase // The "lowercase" check distinguishes locales from other 2-letter strings like "US" @@ -2816,11 +2756,6 @@ var Gmail = function(localJQuery) { }; - api.helper.get.is_delegated_inbox = function() { - return $(".identityUserDelegatedAccount").length === 1; - }; - - api.helper.get.visible_emails_pre = function(customInboxQuery) { var page = api.get.current_page(); var url = window.location.origin + window.location.pathname + "?ui=2&ik=" + api.tracker.ik+"&rid=" + api.tracker.rid + "&view=tl&num=120&rt=1"; From f2aea6183e0d890f3e7298d9a0aa4c6510cf0ca4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jostein=20Kj=C3=B8nigsen?= Date: Thu, 21 Apr 2022 21:24:43 +0200 Subject: [PATCH 2/4] Update changelog --- CHANGELOG.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec3b2a08..fe4ff34c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,29 @@ # Changelog +## Version 2.0.0 - To be released + +Major breaking changes. Removed lots of no longer working functions, +or long ago obsoleted functions or unsupported ways of using Gmail.js. + +High level change in functionality: + +- Using Gmail.js without node/bundling and js "modules" is no longer supported. +- Long obsoleted non-async API-calls where async alternatives exist are now + removed. +- No longer working or fixable API-calls have been removed. + +Functions removed includes: + +- `api.version` +- `api.get.loggedin_accounts()` +- `api.get.delegated_to_email()` +- `api.get.manager_email()` +- `api.helper.get.is_delegated_inbox()` + +This release may hurt a little, but hopefully it should improve the health of +Gmail.js in the long run. + ## Version 1.0.18 - Fix parsing of attachments in emails form embedded JSON. Thanks @onestep! From d9a97cb0b476c1571065e8e3379c63a66493055c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jostein=20Kj=C3=B8nigsen?= Date: Thu, 21 Apr 2022 21:35:30 +0200 Subject: [PATCH 3/4] Obtain jQuery through imports. No more nonsense :) --- src/gmail.d.ts | 2 +- src/gmail.js | 24 +++--------------------- 2 files changed, 4 insertions(+), 22 deletions(-) diff --git a/src/gmail.d.ts b/src/gmail.d.ts index b88d4ee1..d8c989ce 100644 --- a/src/gmail.d.ts +++ b/src/gmail.d.ts @@ -991,7 +991,7 @@ interface GmailCache { //////////////////////////////////////////////////////////////////////////////// declare class Gmail { - constructor(localJQuery?: JQueryStatic); + constructor(); version: string; /** diff --git a/src/gmail.js b/src/gmail.js index ab33388d..1d354fcc 100644 --- a/src/gmail.js +++ b/src/gmail.js @@ -6,28 +6,10 @@ /*eslint-env es6*/ -var Gmail = function(localJQuery) { +import $ from "jquery"; - /* - Use the provided "jQuery" if possible, in order to avoid conflicts with - other extensions that use $ for other purposes. - */ - var $; - if (typeof localJQuery !== "undefined") { - $ = localJQuery; - } else if (typeof jQuery !== "undefined") { - $ = jQuery; - } else { - // try load jQuery through node. - try { - $ = require("jquery"); - } - catch(err) { - // else leave $ undefined, which may be fine for some purposes. - } - } - - var window_opener = typeof (window) !== "undefined" ? window.opener : null; +const Gmail = function() { + let window_opener = typeof (window) !== "undefined" ? window.opener : null; if (window_opener) { try { // access to window.opener domain will fail in case of cross-origin access From 07ba5aaa6c807988ba58688e3c19c5da330c1fa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jostein=20Kj=C3=B8nigsen?= Date: Thu, 21 Apr 2022 21:42:01 +0200 Subject: [PATCH 4/4] Convert "Gmail" object to real export. - Try make it act more like a real class too. --- README.md | 34 +++++++++------------------------- src/gmail.d.ts | 2 +- src/gmail.js | 34 ++++++++++++++-------------------- 3 files changed, 24 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 8505895b..3dc19aa7 100755 --- a/README.md +++ b/README.md @@ -44,38 +44,22 @@ For a ready to use example/boilerplate repo, look no further: - **[GmailJS Node Boilerplate](https://github.com/josteink/gmailjs-node-boilerplate)** - Example for how to create a browser-extension using GmailJS and modern javascript with NodeJS and script-bundling for instant load-times. -### Content Security Policy (legacy advice) +## Usage -In earlier advice given w.r.t. deployment of GmailJS, where scripts were injected one by -one, with cumbersome loading and probing mechanisms, CSP could be an -problem causing your extension to fail if GmailJS was injected incorrectly. - -If you use modern javascript and script-bundling in your extension (like in the boilerplate example), CSP will not interfere with loading -of your extension, nor GmailJS. - -If you have any issues with CSP, the general advice is to build your extension using script-bundling and eliminate the cause of the error -all together. - -While you may be able to make it work, legacy loading is no longer considered supported by GmailJS. - -## Typescript - -Using gmail-js with TypeScript is relatively easy, but if you use normal `import` syntax it -will fail. Instead you need to use `require`-syntax to load it: +Using gmail-js works as normal node-module and you just `import` it into your script: ````typescript -const GmailFactory = require("gmail-js"); -const gmail = new GmailFactory.Gmail() as Gmail; -// working on the gmail-object now provides type-safety. -```` +import { Gmail } from "gmail-js"; -You will also have to import the types somewhere, like in a file called `types.d.ts` in your project: - -````typescript -import "gmail-js"; +const gmail = new Gmail(); +// for typescript, working on the gmail-object automatically provides type-safety. ```` +### Notes on Typescript support +If you want to use gmail-js with TypeScript, you will probably also need to import +additional types in the above import-statement. Don't worry about it, and let your IDE +of choice guide you wrt which types are needed and should be imported :) ## Methods diff --git a/src/gmail.d.ts b/src/gmail.d.ts index d8c989ce..dc953686 100644 --- a/src/gmail.d.ts +++ b/src/gmail.d.ts @@ -990,7 +990,7 @@ interface GmailCache { // //////////////////////////////////////////////////////////////////////////////// -declare class Gmail { +export class Gmail { constructor(); version: string; diff --git a/src/gmail.js b/src/gmail.js index 1d354fcc..c59e165d 100644 --- a/src/gmail.js +++ b/src/gmail.js @@ -8,7 +8,7 @@ import $ from "jquery"; -const Gmail = function() { +export function Gmail() { let window_opener = typeof (window) !== "undefined" ? window.opener : null; if (window_opener) { try { @@ -24,17 +24,19 @@ const Gmail = function() { } } - var api = { - get : {}, - observe : {}, - check : { data: {}}, - tools : {}, - tracker : {}, - dom : {}, - chat : {}, - compose : {}, - helper : {get: {}} - }; + this.get = {}; + this.observe = {}; + this.check = {}; + this.check.data = {}; + this.tools = {}; + this.tracker = {}; + this.dom = {}; + this.chat = {}; + this.compose = {}; + this.helper = {}; + this.helper.get = {}; + + api = this; api.DISABLE_OLD_GMAIL_API_DEPRECATION_WARNINGS = false; @@ -46,7 +48,6 @@ const Gmail = function() { console.warn("GmailJS: using deprecated API for old Gmail.", text); } - api.version = "0.8.0"; api.tracker.globals = typeof GLOBALS !== "undefined" ? GLOBALS : ( @@ -4361,11 +4362,4 @@ const Gmail = function() { if (typeof(document) !== "undefined") { api.tools.embedded_data_watcher(); } - - return api; }; - -// make class accessible to require()-users. -if (typeof(exports) !== "undefined") { - exports.Gmail = Gmail; -}