gmail.js icon indicating copy to clipboard operation
gmail.js copied to clipboard

Gmail change for innerHTML

Open DiegoMMR opened this issue 11 months ago • 36 comments

Gmail will change and add a trustedHTML policy that doenst allow jquery to load

https://workspaceupdates.googleblog.com/2024/01/extending-trusted-types-to-gmail.html

Untitled

to test it if you dont have that change you need to install this extension https://chromewebstore.google.com/detail/modheader-modify-http-hea/idgpnmonknjnojddfkpgkljpfnnfcklj

then you need to add this policy

require-trusted-types-for = 'script'

image

we did a change from innerHTML to setHTML is a temporal fix that worked but prob is not permament

https://developer.mozilla.org/en-US/docs/Web/API/Element/setHTML

but in theory the change to jquery 4 should fix it

https://blog.jquery.com/2024/02/06/jquery-4-0-0-beta/

DiegoMMR avatar Feb 28 '24 20:02 DiegoMMR

Eeek. That sounds like it can cause quite a bit of problems, depending on the complexity of the extension.

Would you be willing to upstream and PRs which helps alleviate the issue for now?

While normally I've tried to avoid breaking changes in Gmail.js, given how this sounds like a fairly breaking change on Google's end, I might be willing to accept PRs which includes breaking changes, if that is required to solve this problem.

josteink avatar Feb 28 '24 20:02 josteink

Talking about how upgraded jQuery solves this... Ive considered for quite a while how it may be time to embrace regular DOM APIs instead of relying on jQuery.

Would using plain DOM APIs help us in this case? Or would we just get the same problems none the less?

josteink avatar Feb 28 '24 21:02 josteink

Eeek. That sounds like it can cause quite a bit of problems, depending on the complexity of the extension.

Would you be willing to upstream and PRs which helps alleviate the issue for now?

While normally I've tried to avoid breaking changes in Gmail.js, given how this sounds like a fairly breaking change on Google's end, I might be willing to accept PRs which includes breaking changes, if that is required to solve this problem.

we have the local files for jquery and gmail.js in our repo... so what we did for now was replace all the innerHTML for setHTML with vscode and for that that worked but bc our extension was a bit big it had that innerHTML in more places but the first place where the error was show was on jquery

DiegoMMR avatar Feb 28 '24 21:02 DiegoMMR

Talking about how upgraded jQuery solves this... Ive considered for quite a while how it may be time to embrace regular DOM APIs instead of relying on jQuery.

Would using plain DOM APIs help us in this case? Or would we just get the same problems none the less?

should work using plain DOM APIs i think... bc the problem with that change is on the innetHTML that can also be changed for other things and google provide this https://web.dev/articles/trusted-types?hl=es#rewrite_the_offending_code

DiegoMMR avatar Feb 28 '24 21:02 DiegoMMR

@onestep said:

Taking into account that jQuery could not be used anymore due to recent TrustedHTML changes in Gmail, would it make sense to avoid using it altogether for DOM manipulations?

Making that change is a major compatibility break. I'm generally against making breaking changes when one doesn't have to, because usually it involves more work for everyone.

If we are to make a breaking change it should be done properly:

  • Implementation has to be changed
  • Type-script signatures has to be updated
  • Documentation has to be updated
  • Changes will need to be regression-tested

If we do this now, it might save us effort down the line... But as mentioned above, there might be even more hurdles down the line.

If so, would also this work be worth it, if it has to be redone again soonish?

In that case, maybe not doing a breaking change but just updating jQuery to latest beta is an acceptable "middle-ground" while seeing how things play out?

Opinions?

josteink avatar Feb 29 '24 12:02 josteink

Hello @josteink,

As a super quick fix to allow running on jQuery 4, I've prepared a PR to avoid using deprecated jQuery helpers - #780. That worked for me when running on jQuery 4 beta.

onestep avatar Feb 29 '24 15:02 onestep

Thank you @DiegoMMR for this extension suggestion to test, Google started to roll out changes to us, but I haven't received it. Thanks to this thread, I was able to replicate a problem and fix it very quickly.

For what it's worth, I migrated to new jQuery 4 beta and added only following changes to the codebase, in my case working fine with gmail.js

jQuery.isArray = Array.isArray;
jQuery.extend({
  htmlPrefilter: createTrustedHTML // Create "magical" trusted HTML as in https://web.dev/articles/trusted-types#create_a_trusted_type_policy
});
this.gmail = new Gmail(jQuery);

huksley avatar Mar 01 '24 12:03 huksley

thanks to everyone here who jumped in to solve this problem!! I've been hit hard, my gmail.js-based extension totally stopped working over the past few days......! but I followed the instructions here, upgraded to jquery 4 beta, changed all of my innerhtml to actual jquery objects, etc.

fingers crossed for the extension review process but yeah. just wanted to give a huge huge thanks. cheers

gregsadetsky avatar Mar 05 '24 04:03 gregsadetsky

Thank you @DiegoMMR for this extension suggestion to test, Google started to roll out changes to us, but I haven't received it. Thanks to this thread, I was able to replicate a problem and fix it very quickly.

For what it's worth, I migrated to new jQuery 4 beta and added only following changes to the codebase, in my case working fine with gmail.js

jQuery.isArray = Array.isArray;
jQuery.extend({
  htmlPrefilter: createTrustedHTML // Create "magical" trusted HTML as in https://web.dev/articles/trusted-types#create_a_trusted_type_policy
});
this.gmail = new Gmail(jQuery);

Although this fix renders the extension content, the trustedHtml error still occurs in Gmail.api.tools.add_toolbar_button failing to render the button in gmail's toolbar for me, is anyone else also facing this issue?

mohammedfarhan99 avatar Mar 07 '24 06:03 mohammedfarhan99

New version published to npmjs with preliminary changes as version 1.1.13.

josteink avatar Mar 07 '24 08:03 josteink

Thank you @DiegoMMR for this extension suggestion to test, Google started to roll out changes to us, but I haven't received it. Thanks to this thread, I was able to replicate a problem and fix it very quickly.

For what it's worth, I migrated to new jQuery 4 beta and added only following changes to the codebase, in my case working fine with gmail.js

jQuery.isArray = Array.isArray;

jQuery.extend({

htmlPrefilter: createTrustedHTML // Create "magical" trusted HTML as in https://web.dev/articles/trusted-types#create_a_trusted_type_policy

});

this.gmail = new Gmail(jQuery);

Although this fix renders the extension content, the trustedHtml error still occurs in Gmail.api.tools.add_toolbar_button failing to render the button in gmail's toolbar for me, is anyone else also facing this issue?

The HTML you pass in to the function needs to be converted into "trusted" html using the same technique as the htmlPrefilter for Jquery.

I've tested that in my extension and that works without any issues.

josteink avatar Mar 07 '24 16:03 josteink

Hello,

Did you have observe on "compose" work ?

skyderman avatar Mar 07 '24 17:03 skyderman

Hello,

Did you have observe on "compose" work ?

I'm having issues with my compose modules too, but haven't had time to look into how/why that's failing yet.

josteink avatar Mar 07 '24 18:03 josteink

The issue with observers is this return statement:

    var classes = cn.trim ? cn.trim().split(/\s+/) : []
    if (!classes.length) classes.push("") // if no class, then check for anything observing nodes with no class
    console.log("classes", classes)
    for (let className of classes) {
      var observers = dom_observer_map[className]
      console.log("asd", className)
      if (className === "An") {
        console.log("observers", observers)
      }
      if (!observers) {
        return
      }

For whatever reason An is now the second class, and the first class has no observers so it just returns. I think it should be continue not return...

MadcowD avatar Mar 08 '24 01:03 MadcowD

Thank you @DiegoMMR for this extension suggestion to test, Google started to roll out changes to us, but I haven't received it. Thanks to this thread, I was able to replicate a problem and fix it very quickly.

For what it's worth, I migrated to new jQuery 4 beta and added only following changes to the codebase, in my case working fine with gmail.js

jQuery.isArray = Array.isArray;

jQuery.extend({

htmlPrefilter: createTrustedHTML // Create "magical" trusted HTML as in https://web.dev/articles/trusted-types#create_a_trusted_type_policy

});

this.gmail = new Gmail(jQuery);

Although this fix renders the extension content, the trustedHtml error still occurs in Gmail.api.tools.add_toolbar_button failing to render the button in gmail's toolbar for me, is anyone else also facing this issue?

The HTML you pass in to the function needs to be converted into "trusted" html using the same technique as the htmlPrefilter for Jquery.

I've tested that in my extension and that works without any issues.

Yes it works, I had made a silly mistake, I had passed the createTrustedHtml logic as an arrow function, which for some reason jQuery was not able to override because of lexical scoping ig, passing createTrustedHtml logic as function(string){} instead of (string) =>{} solved it

mohammedfarhan99 avatar Mar 08 '24 06:03 mohammedfarhan99

The issue with observers is this return statement:

    var classes = cn.trim ? cn.trim().split(/\s+/) : []
    if (!classes.length) classes.push("") // if no class, then check for anything observing nodes with no class
    console.log("classes", classes)
    for (let className of classes) {
      var observers = dom_observer_map[className]
      console.log("asd", className)
      if (className === "An") {
        console.log("observers", observers)
      }
      if (!observers) {
        return
      }

For whatever reason An is now the second class, and the first class has no observers so it just returns. I think it should be continue not return...

@josteink I'm having the same issue with the compose event. Indeed, while return of a non-false value functioned for $.each as continue, it's no longer the case in a for...of. image

Created following PR for this.

kinkoazc avatar Mar 08 '24 09:03 kinkoazc

FYI this PR is merged and now available in v1.1.14.

josteink avatar Mar 08 '24 15:03 josteink

@kinkoazc thanks, my bad that I haven't checked for returns inside forEach when I converted them to for...of. 🤦

onestep avatar Mar 09 '24 11:03 onestep

@kinkoazc thanks, my bad that I haven't checked for returns inside forEach when I converted them to for...of. 🤦

No worries. Mistakes happens.

You made some huge improvements which helped everyone in the community and the community helped you back.

It's how open-source is supposed to work 🙂

josteink avatar Mar 09 '24 11:03 josteink

I'm still having trouble with this. I updated to the latest version of gmail-js and jquery, and I added this to my gmailJsLoader.js file:

const createTrustedHTML = trustedTypes.createPolicy("default", {
  createHTML: (to_escape) => to_escape,
});

jQuery.isArray = Array.isArray;
jQuery.extend({
  htmlPrefilter: createTrustedHTML, // Create "magical" trusted HTML as in https://web.dev/articles/trusted-types#create_a_trusted_type_policy
});

But I'm getting this error:

Uncaught TypeError: jQuery3.extend is not a function
Context
https://mail.google.com/mail/u/0/#inbox
Stack Trace
dist/gmailJsLoader.js:5652 (anonymous function)
dist/gmailJsLoader.js:5657 (anonymous function)

kaeevans avatar Mar 14 '24 21:03 kaeevans

You need the jquery 4 beta.

josteink avatar Mar 14 '24 21:03 josteink

I have the jQuery 4 beta. My package.json says "jquery": "^4.0.0-beta", And when I add a breakpoint and run jQuery.fn.jquery in the console it says '4.0.0-beta'. But it still seems to be using jQuery3 somehow

kaeevans avatar Mar 14 '24 21:03 kaeevans

I don't see you importing/requiring jquery as a package in that example.

Maybe that's what you're missing?

josteink avatar Mar 14 '24 22:03 josteink

Here's the entire file:

const GmailFactory = require("gmail-js");
const jQuery = require("jquery");

const createTrustedHTML = trustedTypes.createPolicy("default", {
  createHTML: (to_escape) => to_escape,
});

jQuery.isArray = Array.isArray;
jQuery.extend({
  htmlPrefilter: createTrustedHTML, // Create "magical" trusted HTML as in https://web.dev/articles/trusted-types#create_a_trusted_type_policy
});

// don't mess up too bad if we have several gmail.js-based
// extensions loaded at the same time!
window._gmailjs = window._gmailjs || new GmailFactory.Gmail(jQuery);

kaeevans avatar Mar 14 '24 22:03 kaeevans

Not sure if thats your only error, but I at least spotted this tiny thing:

const createTrustedHTML = trustedTypes.createPolicy("default", {
  createHTML: (to_escape) => to_escape,
});

jQuery.extend({
  htmlPrefilter: createTrustedHTML
});

Here you are passing the whole trusted HTML policy in to jquery, which simply expects a function to convert string to trusted strings.

Use this instead:

const trustedHTMLpolicy = trustedTypes.createPolicy("default", {
  createHTML: (to_escape) => to_escape,
});

jQuery.extend({
  htmlPrefilter: trustedHTMLpolicy.createHTML // this is the actual function which jQuery needs
});

josteink avatar Mar 15 '24 09:03 josteink

GmailJS Node Boilerplate gives me an error. These are the steps followed:

  1. git clone https://github.com/josteink/gmailjs-node-boilerplate/
  2. cd gmailjs-node-boilerplate
  3. npm install
  4. npm update
  5. Edit the package.json file and put "jquery": "^4.0.0-beta"
  6. npm run build

And finally I load the extension. When I open Gmail in Chrome I get the following error:

Uncaught TypeError: $ is not a function at Gmail.api.dom.inbox_content (gmail.js:316:16) at Gmail.api.observe.on_dom (gmail.js:2733:24) at Gmail.api.observe.on (gmail.js:2329:24) at startExtension (extension.js:18:19) at extension.js:10:5

Could you please help me to correct the error?

guzman-rc avatar Mar 15 '24 13:03 guzman-rc

I intended to update the boilerplate but never got around to it, and forgot it completely.

Should be updated now. Tested and works.

josteink avatar Mar 15 '24 14:03 josteink

Great, now it works but I get the following output: Hello, null. This is your extension talking!

This function is not working properly: const userEmail = gmail.get.user_email();

guzman-rc avatar Mar 15 '24 14:03 guzman-rc

Weird. Works for me.

Howeever, it seems like most things are up and running as they should now, so I just would file that as an individual bug, and see if someone can come up with a PR to fix it.

josteink avatar Mar 15 '24 15:03 josteink

It seems that the error occurs with personal accounts: @gmail.com, with professional accounts it works correctly.

To fix this error, add these instructions:

if(api.tracker.globals.length == 0 && GLOBALS !== "undefined" && GLOBALS.length > 11)
     api.tracker.globals = GLOBALS;

to the function: api.get.user_email

api.get.user_email = function() {
  if(api.tracker.globals.length == 0 && GLOBALS !== "undefined" && GLOBALS.length > 11)
    api.tracker.globals = GLOBALS;
		
  let user_email = api.tracker.globals[10];	  

  if (user_email) {
    return user_email;
  }
  const elements = document.getElementsByClassName("eYSAde");
  for (const el of elements) {
    if (el.innerHTML.indexOf("@") === -1) {
      return el.innerHTML;
    }
  }
      return null;
};

guzman-rc avatar Mar 15 '24 17:03 guzman-rc