Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I'm trying to js script but it does not seem to work #17

Closed
ondrobaco opened this issue Oct 15, 2023 · 9 comments
Closed

I'm trying to js script but it does not seem to work #17

ondrobaco opened this issue Oct 15, 2023 · 9 comments

Comments

@ondrobaco
Copy link

Hi, thanks for a great extension.
I'm trying putting the following script under a short cut with the extension but it does not seem to execute.
https://gist.github.com/alangrainger/ef52d535f7b1cc4c0c1b23da4e91806d
It works fine when the script is used in bookmark.
I tried to put the code under script as well as url, but none of the options work.

Would you know where could be the problem?

@onozaty
Copy link
Owner

onozaty commented Oct 17, 2023

Hi @ondrobaco

It seems to be because navigator.clipboard.writeText is not working in the extension.
This seems to be a Chrome issue.

As an alternative, i have rewritten the script with reference to the following.

function copyTextToClipboard(text) {
  //Create a textbox field where we can insert text to. 
  var copyFrom = document.createElement("textarea");

  //Set the text content to be the text you wished to copy.
  copyFrom.textContent = text;

  //Append the textbox field into the body as a child. 
  //"execCommand()" only works when there exists selected text, and the text is inside 
  //document.body (meaning the text is part of a valid rendered HTML element).
  document.body.appendChild(copyFrom);

  //Select all the text!
  copyFrom.select();

  //Execute command
  document.execCommand('copy');

  //(Optional) De-select the text using blur(). 
  copyFrom.blur();

  //Remove the textbox field from the document.body, so no other JavaScript nor 
  //other elements can get access to this.
  document.body.removeChild(copyFrom);
}

messageId = document.querySelector('div[data-legacy-message-id]').getAttribute('data-legacy-message-id');
user = location.pathname.match(/\/u\/(\d)/)[1];
copyTextToClipboard('https://mail.google.com/mail/u/' + user + '/#inbox/' + messageId);

It is working fine at hand.
Could you please give it a try?

Thanks.

@ondrobaco
Copy link
Author

Thank you, it works! Forever grateful :)!

@onozaty
Copy link
Owner

onozaty commented Oct 17, 2023

You're welcome!
If you would like, I would be happy to give you a Star on GitHub or a review in the Chrome web store 😉

Thanks.

@onozaty onozaty closed this as completed Oct 17, 2023
@ondrobaco
Copy link
Author

Hi onozaty,
There were some changes in how how the scripts work. I'm trying to get the code working again it is an immensely useful piece of code for me. I followed the "how to specify script" mini tutorial that you have on the page but no success. Would you be so kind and help me with the code to put into user-scripts.js to make the gmail link copy work again? thank you kindly

@onozaty
Copy link
Owner

onozaty commented Jun 26, 2024

I will send you a set of files that I have verified to work.
Please unzip and load this.

clipboardWrite is added to the manifest.json permissions.
The script uses navigator.clipboard.writeText.

Thanks.

@ondrobaco
Copy link
Author

ondrobaco commented Jun 26, 2024 via email

@onozaty
Copy link
Owner

onozaty commented Jun 26, 2024

Instead of overwriting the extensions folder, could you please load the folder where you extracted the src.zip with Load Unpacked Extension?

If you have already installed it from the Chrome Web Store, please disable it.

@ondrobaco
Copy link
Author

ondrobaco commented Jun 26, 2024 via email

@onozaty
Copy link
Owner

onozaty commented Jun 26, 2024

If you haven't written a review on the Chrome Web Store, I would appreciate it if you would.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants