How to execute JS on startup? I have a "copy on select" userscript

183 views
Skip to first unread message

Lin Onetwo

unread,
Apr 11, 2020, 3:08:15 AM4/11/20
to tiddl...@googlegroups.com
I found $:/tags/StartupAction/Browser will only execute tiddlywi actions, but can't execute javascript.

But I have following JS want to execute:

// we won't do copy on select on text editor, otherwise you can't select and override text in the editor or text input
function checkIfElementIsEditor(element) {
  if (!element || !element.className || !element.className.toLowerCase || !element.nodeName) return false;
  const isEditableElement = ['INPUT', 'TEXTAREA'].includes(element.nodeName);

  const isTextEditor = element.className.toLowerCase().includes('codemirror');

  return isEditableElement || isTextEditor;
}
// Copy on select, copy document selection when mouse button is up
document.addEventListener('mouseup', function onMouseUp() {
  const elementsUnderMouse = document.querySelectorAll(':hover');

  if (!elementsUnderMouse || Array.from(elementsUnderMouse).some(checkIfElementIsEditor)) return;
  document.execCommand('copy');
});


What could I do?

I'm currently using WebcataLog to inject Userscript before wiki loading

Eric Shulman

unread,
Apr 11, 2020, 4:26:04 AM4/11/20
to TiddlyWiki
On Saturday, April 11, 2020 at 12:08:15 AM UTC-7, Lin Onetwo wrote:
I found $:/tags/StartupAction/Browser will only execute tiddlywi actions, but can't execute javascript.
But I have following JS want to execute:
What could I do?
I'm currently using WebcataLog to inject Userscript before wiki loading

1) added <script> ... </script> around your code
2) tag the tiddler with $:/tags/RawMarkup
3) save-and-reload your document

What this does is to add the content of the tagged tiddler to the bottom of the <head> section of the document.
This allows your JS code to be invoked as soon as the document is loaded, even before the TWCore wiki is initialized


enjoy,
-e

Mohammad

unread,
Apr 11, 2020, 9:13:04 AM4/11/20
to TiddlyWiki
Hi @Lin
 Nice code! I tested as Eric explained and works great!
A good functionality!

--Mohammad

Lin Onetwo

unread,
Apr 11, 2020, 11:30:53 AM4/11/20
to TiddlyWiki
Thank you Eric, that works!

And @Mohammad I have updated my userscript, fix some bug in it

// we won't do copy on select on text editor, otherwise you can't select and override text in the editor or text input
function checkIfElementIsEditor(element) 
{
    if (!element || !element.nodeName) return false;
  const isEditableElement = ['INPUT', 'TEXTAREA', 'BUTTON'].includes(element.nodeName);
    if (!isEditableElement) {
  	if (!element.className || !element.className.toLowerCase) return false;
    }
  
const isTextEditor = element.className.toLowerCase().includes('codemirror');

  return isEditableElement || isTextEditor;
}
// if we start selection on editor, we prevent the following execution of this script
let copyOnSelectPreventNextCopy = false;
document.addEventListener('mousedown', function onMouseDown() {
  
const elementsUnderMouse = document.querySelectorAll(':hover'
);

  if (!elementsUnderMouse || Array.from(elementsUnderMouse).some(checkIfElementIsEditor)) {
    copyOnSelectPreventNextCopy = true;
  }
});
// Copy on select, copy document selection when mouse button is up
document.addEventListener('mouseup', function onMouseUp() {
  const elementsUnderMouse = document.querySelectorAll(':hover');

  if
 (
    copyOnSelectPreventNextCopy ||
    !elementsUnderMouse ||
    Array.from(elementsUnderMouse).some(checkIfElementIsEditor)
  ) {
    copyOnSelectPreventNextCopy = false;
    
return;
  }
  document.execCommand('copy');
});

在 2020年4月11日星期六 UTC+8下午4:26:04,Eric Shulman写道:

Mohammad Rahmani

unread,
Apr 11, 2020, 11:47:05 AM4/11/20
to tiddl...@googlegroups.com
Thanks Lin,
I added this to TW-Scripts (will appear next month online)


Best wishes
Mohammad


--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/56f2c48e-412c-4a76-a7a1-a6751652908a%40googlegroups.com.

TonyM

unread,
Apr 12, 2020, 3:31:20 AM4/12/20
to TiddlyWiki
So what mysterious function does this achieve?

tony

Lin Onetwo

unread,
Apr 12, 2020, 3:35:30 AM4/12/20
to TiddlyWiki

I'm using desktop app with nodejs tiddlywiki, so I feel unconfertable without this firefox add-on. So I make it work in tiddlywiki.

在 2020年4月12日星期日 UTC+8下午3:31:20,TonyM写道:

TonyM

unread,
Apr 12, 2020, 3:51:45 AM4/12/20
to TiddlyWiki
Thanks for Sharing.

I am great with TiddlyWiki but not the Javscript side of things, I appreciate such contributions.

Regards
Tony

maki aea

unread,
Jan 2, 2021, 6:58:32 AM1/2/21
to TiddlyWiki
love this, thanks lin onetwo! i'm on mobile so i modded a copy slightly to use pointer instead of mouse, sorry am not able to test extensively or on desktop to see if it still works there just now: http://makiaea.org/00045/20201205makiaea-anwiki#copy%20on%20select

warmest wishes, maki

Sylvain Naudin

unread,
Jan 2, 2021, 7:21:54 AM1/2/21
to TiddlyWiki
Hello Maki,

Thank you for putting this thread back in recent items, it allows me to discover the solution. Yours seems to work correctly on desktop (Firefox under Fedora Gnu/Linux).

Regards,
Sylvain

maki aea

unread,
Jan 7, 2021, 10:41:26 AM1/7/21
to TiddlyWiki
thanks sylvain! it's quite hard to find things in the group, if we didn't come across it while it's popular or if we don't know even what kind of thing to look for in say david's toolmap. it seems like a very common knowledge issue that we often don't know how to find things we don't know about— i find it an interesting thing to think about and maybe to solve a little!. happy new year!
Reply all
Reply to author
Forward
0 new messages