# Useful javascript snippets for the editor

**URL:** <https://forums.ankiweb.net/t/useful-javascript-snippets-for-the-editor/14536>\
**Category:** Add-ons\
**Created:** [October 30, 2021, 5:04pm UTC](https://forums.ankiweb.net/t/useful-javascript-snippets-for-the-editor/14536 "2021-10-30T17:04:37Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![kleinerpirat](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/kleinerpirat/32/19863_2.png) [@kleinerpirat](https://forums.ankiweb.net/u/kleinerpirat)\
**Post date:** [April 28, 2022, 7:11pm UTC](https://forums.ankiweb.net/t/useful-javascript-snippets-for-the-editor/14536/21 "2022-04-28T19:11:51Z")

</div>

I could submit a PR to New Format Pack that implements these two ideas of yours @MIZMU @gnomek.

Of course, I’d also implement a config so you can choose which buttons you want.

---

<div class="post-metadata">

**Author:** ![gnomek](https://avatars.discourse-cdn.com/v4/letter/g/e79b87/32.png) [@gnomek](https://forums.ankiweb.net/u/gnomek)\
**Post date:** [April 29, 2022, 8:37am UTC](https://forums.ankiweb.net/t/useful-javascript-snippets-for-the-editor/14536/22 "2022-04-29T08:37:30Z")

</div>

@ [MIZMU](https://forums.ankiweb.net/u/MIZMU)  
Check also this add-on:  
[Custom Styles (font color, background colour, classes)](https://ankiweb.net/shared/info/1899278645)

@[kleinerpirat](https://forums.ankiweb.net/u/kleinerpirat)  
Thank you very much.

---

<div class="post-metadata">

**Author:** ![TRIAEIOU](https://avatars.discourse-cdn.com/v4/letter/t/df788c/32.png) [@TRIAEIOU](https://forums.ankiweb.net/u/TRIAEIOU)\
**Post date:** [May 24, 2022, 6:36am UTC](https://forums.ankiweb.net/t/useful-javascript-snippets-for-the-editor/14536/23 "2022-05-24T06:36:48Z")

</div>

As @kleinerpirat pointed out in another thread: useful snippet for adding custom keyboard shortcuts in the new editor (in Editor Scripts & Symbols form):  
`{
“File”: “”,
“Language”: “JS”,
“Post”: “”,
“Pre”: “(function(){const btn = document.querySelector(\”[title^=‘Increase indent’]\"); const state = btn.disabled; btn.disabled = false, btn.click(), btn.disabled = state;})();",
“Script”: “Indent”,
“Shortcut”: “Ctrl+shift+:”
}`

Handy for those of us with keyboards that have symbols in the ‘wrong places’, for instance keyboards where colon is ‘shift dot’ making it impossible to press ‘ctrl+shift+.’

---

<div class="post-metadata">

**Author:** ![MIZMU](https://avatars.discourse-cdn.com/v4/letter/m/7993a0/32.png) [@MIZMU](https://forums.ankiweb.net/u/MIZMU)\
**Post date:** [May 26, 2022, 11:21pm UTC](https://forums.ankiweb.net/t/useful-javascript-snippets-for-the-editor/14536/24 "2022-05-26T23:21:25Z")

</div>

ok. this for font size:

```auto
 setFormat("fontSize", "7")

```

But it is only limited to a size between 1-7  
I tried the following solution ([source](https://www.demo2s.com/javascript/javascript-document-execcommand-change-font-size-in-pixels.html)):

```auto
<script>
function changeFont() {
 
   setFormat("fontSize", "7");
 
   var fontElements = document.getElementsByTagName("font");
 
   for (var i = 0, len = fontElements.length; i < len; ++i) {
 
       if (fontElements[i].size == "7") {
 
           fontElements[i].removeAttribute("size");
 
           fontElements[i].style.fontSize = "30px";
			
       }
 
   }
  
}
</script>

```

But it unfortunately did not work.

Also, I tried to perform the same method on the font family and it also did not work.:

```auto
setFormat('fontName', "Arial");

```

I’m waiting for the experts …

---

<div class="post-metadata">

**Author:** ![jambamboleo](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@jambamboleo](https://forums.ankiweb.net/u/jambamboleo)\
**Post date:** [June 23, 2022, 8:52am UTC](https://forums.ankiweb.net/t/useful-javascript-snippets-for-the-editor/14536/25 "2022-06-23T08:52:45Z")

</div>

It’s updated now (new upload): [Remove Cloze Button and Hotkey - AnkiWeb](https://ankiweb.net/shared/info/1966460736)

---

<div class="post-metadata">

**Author:** ![TRIAEIOU](https://avatars.discourse-cdn.com/v4/letter/t/df788c/32.png) [@TRIAEIOU](https://forums.ankiweb.net/u/TRIAEIOU)\
**Post date:** [June 24, 2022, 6:54am UTC](https://forums.ankiweb.net/t/useful-javascript-snippets-for-the-editor/14536/26 "2022-06-24T06:54:36Z")

</div>

Without looking into it too much, I think the “current” approach would be to wrap the text in a \<span\>-tag and set the font size with CSS: `font-size: 30px; font-family: Arial;` . The \<font\>-tag is deprecated ([\<font\> - HTML: HyperText Markup Language | MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/font)). You can get the calculated font-size (taking all CSS into account) with `window.getComputedStyle()`, i.e. something like `window.getComputedStyle(document.activeElement.shadowRoot.firstElementChild).fontSize`. So maybe do something like:

1. Wrap selection in a span tag with a unique ID so that you know “it’s yours” (I usually use the UUID module for that)
2. Get the computedStyle() for the span
3. Populate your popup menu
4. Apply the new font size directly to the span style

Now, having said that it is going to be a bit more tricky when you mix in that your selection might bridge other HTML tags, for instance start in one \<li\> and end in another - this will require splitting the \<span\> into two etc.

---

<div class="post-metadata">

**Author:** ![MIZMU](https://avatars.discourse-cdn.com/v4/letter/m/7993a0/32.png) [@MIZMU](https://forums.ankiweb.net/u/MIZMU)\
**Post date:** [June 27, 2022, 7:35pm UTC](https://forums.ankiweb.net/t/useful-javascript-snippets-for-the-editor/14536/27 "2022-06-27T19:35:08Z")

</div>

I ended up using:

```auto
f"setFormat('{style}', '{val}');"
style= fontName/fontSize

```

---

<div class="post-metadata">

**Author:** ![chano](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/chano/32/11057_2.png) [@chano](https://forums.ankiweb.net/u/chano)\
**Post date:** [September 22, 2022, 8:45am UTC](https://forums.ankiweb.net/t/useful-javascript-snippets-for-the-editor/14536/28 "2022-09-22T08:45:29Z")

</div>

I want this too…! I just want to change the size of the font in the editor so I can easily emphasize the point(I think bold and other styles like underline are not good or distinct enough), but I’m not familiar with html and js. I tried to change the “Uppercase” script but it failed, I know it’s not hard but can someone please tell me how to do it? Really Thanks!  
BTW, I just want to implement it as it is in Ankidroid like this:  
There’s a button T(Here it needs to select and right click using this plugin, but thai’s ok 🙂 ), you can click it and make the text you selected bigger or smaller.  
(And the Custom Styles (font color, background colour, classes) addon didn’t work for me.)

---

<div class="post-metadata">

**Author:** ![kleinerpirat](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/kleinerpirat/32/19863_2.png) [@kleinerpirat](https://forums.ankiweb.net/u/kleinerpirat)\
**Post date:** [September 22, 2022, 4:20pm UTC](https://forums.ankiweb.net/t/useful-javascript-snippets-for-the-editor/14536/29 "2022-09-22T16:20:48Z")

</div>

> [@chano](#):
>
> bold and other styles like underline are not good or distinct enough)

You can change the appearance of bold text (= content within `<b>` tags) with CSS in your note template:

```auto
b {
  font-size: xx-large;
  font-family Comic Sans;
  text-transform: uppercase;
  /* etc.. */
}

```

---

<div class="post-metadata">

**Author:** ![chano](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/chano/32/11057_2.png) [@chano](https://forums.ankiweb.net/u/chano)\
**Post date:** [September 22, 2022, 5:50pm UTC](https://forums.ankiweb.net/t/useful-javascript-snippets-for-the-editor/14536/30 "2022-09-22T17:50:52Z")

</div>

Yeah, but that’ll have an effect on the existing card content, and there’s a lot of content in the cards. And what I wanna do is to get a single function or snippet, to change the font size freely.

---

<div class="post-metadata">

**Author:** ![TRIAEIOU](https://avatars.discourse-cdn.com/v4/letter/t/df788c/32.png) [@TRIAEIOU](https://forums.ankiweb.net/u/TRIAEIOU)\
**Post date:** [October 16, 2022, 9:24am UTC](https://forums.ankiweb.net/t/useful-javascript-snippets-for-the-editor/14536/31 "2022-10-16T09:24:38Z")

</div>

In [Editor Scripts & Symbols] format: Scroll the entire fields area from keyboard

```
  {
   "1. Menu": "Scroll",
   "3. Items": [
    {
     "1. Script": "Up",
     "2. Shortcut": "Ctrl+Alt+Up",
     "3. Language": "JS",
     "4. Pre": "document.querySelector('div.fields').scrollTop -= 200"
    },
    {
     "1. Script": "Down",
     "2. Shortcut": "Ctrl+Alt+Down",
     "3. Language": "JS",
     "4. Pre": "document.querySelector('div.fields').scrollTop += 200"
    },
    {
     "1. Script": "Left",
     "2. Shortcut": "Ctrl+Alt+Left",
     "3. Language": "JS",
     "4. Pre": "document.querySelector('div.fields').scrollLeft -=200"
    },
    {
     "1. Script": "Right",
     "2. Shortcut": "Ctrl+Alt+Right",
     "3. Language": "JS",
     "4. Pre": "document.querySelector('div.fields').scrollLeft += 200"
    },
    {
     "1. Script": "Home",
     "2. Shortcut": "Ctrl+Alt+Home",
     "3. Language": "JS",
     "4. Pre": "document.querySelector('div.fields').scrollTo(0, 0)"
    },
    {
     "1. Script": "End",
     "2. Shortcut": "Ctrl+Alt+End",
     "3. Language": "JS",
     "4. Pre": "let el = document.querySelector('div.fields'); el.scrollTo(el.scrollWidth, el.scrollHeight)"
    }
   ]
  }

```

---

<div class="post-metadata">

**Author:** ![blyatbeauty](https://avatars.discourse-cdn.com/v4/letter/b/838e76/32.png) [@blyatbeauty](https://forums.ankiweb.net/u/blyatbeauty)\
**Post date:** [March 21, 2024, 1:50pm UTC](https://forums.ankiweb.net/t/useful-javascript-snippets-for-the-editor/14536/32 "2024-03-21T13:50:05Z")

</div>

I added a sentence case function to speed up my workflow, sharing it here for anyone else who could benefit from it. Sorry for the ugly formatting but I needed to make sure all the escape characters appear as needed:

`{ "1. Script": "Sentence case", "2. Shortcut": "Shift+Alt+S", "3. Language": "JS", "4. Pre": "(function(){const sel = document.activeElement.shadowRoot.getSelection();for(i=0;i<sel.rangeCount;i++){const rng = sel.getRangeAt(i);let input = new XMLSerializer().serializeToString(rng.extractContents());input = input.toLowerCase();input = input.replace(/(^|\\.\\s+|\\!\\s+|\\?\\s+)(\\w)/g,function(c){return c.toUpperCase();});rng.insertNode(rng.createContextualFragment(input));sel.addRange(rng);}})();", "5. File": "", "6. Post": "" }`

[Previous page](https://forums.ankiweb.net/t/useful-javascript-snippets-for-the-editor/14536.md?page=1)
