FooSoft / yomichan

Japanese pop-up dictionary extension for Chrome and Firefox.
https://foosoft.net/projects/yomichan
Other
1.07k stars 226 forks source link

[Question] Is it possible to clear 'clipboardText' inside the Anki Template JS handlebars? #2051

Closed aiko-tanaka closed 2 years ago

aiko-tanaka commented 2 years ago

Was trying to find a way to automate having yomichan select between {sentence} and {clipboard-text} where yomichan would prefer {clipboard-text} if it contains something.

{{#*inline "clipboard-text"}}
    {{~#if (hasMedia "clipboardText")}}
    {{#regexReplace "\n(?!$)" "<br>"}}{{#getMedia "clipboardText"}}{{/getMedia}}{{/regexReplace}}
    {{~else~}}
    {{~#if definition.cloze}}{{definition.cloze.sentence}}{{/if~}}
    {{/if~}}
{{/inline}}

I guess it did work with this code. Only problem is everytime I add a new card, the last thing I copied to my clipboard will always be set as the sentence. So I guess one way to fix it would be to have the {{inline}} block clear the clipboard at the end of the code, but I'm not sure if that's possibe. Is it?

aiko-tanaka commented 2 years ago

found a workaround

toasted-nutbread commented 2 years ago

Just in case someone else stumbles upon this, the solution is probably similar to what was mentioned in https://github.com/FooSoft/yomichan/issues/2097#issuecomment-1079830052:

{{#*inline "clipboard-text"}}
    {{~#if
      (op
        "&&"
        (hasMedia "clipboardText")
        (op "!==" (getMedia "clipboardText") "")
      )
    }}
        {{#regexReplace "\n(?!$)" "<br>"}}{{#getMedia "clipboardText"}}{{/getMedia}}{{/regexReplace}}
    {{~else~}}
        {{~#if definition.cloze}}{{definition.cloze.sentence}}{{/if~}}
    {{/if~}}
{{/inline}}

(Spacing in {{#if}} for clarity)