xxhk.org

学习骇客

User Action 1~8 的用法

2024 / 8 / 15

官网 对此也是语焉不详,论坛 的讨论也很少,

方法

在模板里插入``标签对,在其中写这样的函数:

var userJs1 = function() { };

或这样的箭头函数:

var userJs2 = () => document.writeln("hello AnkiDroid,");

整体效果:

<script> var userJs1 = undefined var userJs2 = undefined var userJs3 = undefined var userJs4 = undefined var userJs5 = undefined var userJs6 = undefined var userJs7 = undefined var userJs8 = undefined </script>

用例

定义函数,调用函数。

通过 inspect 插件,在 Anki 复习界面找函数,然后调用。

更多

  1. 显示提示(未验证
function showHint() { var hints = document.querySelectorAll('a.hint'); for (var i = 0; i < hints.length; i++) { if (hints[i].style.display != 'none') { hints[i].click(); break; } } }
  1. 显示提示(未验证
<script> /* User Action: Toggle hint fields */ var userJs1 = () => { for (const link of document.querySelectorAll("a.hint")) { const hint = link.nextElementSibling; if (hint.style.display === "none") { link.style.display = "none"; hint.style.display = "block"; } else { link.style.display = "block"; hint.style.display = "none"; } } }; </script>
  1. 其他案例(未验证
<!-- version 74c200d --> <script> // ############## USER CONFIGURATION START ############## // Auto flip to back when One by one mode. var autoflip = true // ############## TAG SHORTCUT ############## var toggleTagsShortcut = "C"; // ENTER THE TAG TERM WHICH, WHEN PRESENT, WILL TRIGGER A RED BACKGROUND var tagID = "XXXYYYZZZ" // WHETHER THE WHOLE TAG OR ONLY THE LAST PART SHOULD BE SHOWN var numTagLevelsToShow = 0; // ############## USER CONFIGURATION END ############## </script> <!-- AUTO FLIP FRONT --> <script> if (autoflip) { // avoid flickering. Must unset this in the back. document.getElementById("qa").style.display = "none"; if (window.pycmd) { pycmd("ans") } else if (window.showAnswer) { showAnswer() } } // AnkiMobile JS API doesn't have one for show answer. // Best alternative is to use Taps/Swipes to show answer. </script> <!-- Shortcut Matcher Function --> <script> var specialCharCodes = { "-": "minus", "=": "equal", "[": "bracketleft", "]": "bracketright", ";": "semicolon", "'": "quote", "`": "backquote", "\\": "backslash", ",": "comma", ".": "period", "/": "slash", }; // Returns function that match keyboard event to see if it matches given shortcut. function shortcutMatcher(shortcut) { let shortcutKeys = shortcut.toLowerCase().split(/[+]/).map(key => key.trim()) let mainKey = shortcutKeys[shortcutKeys.length - 1] if (mainKey.length === 1) { if (/\d/.test(mainKey)) { mainKey = "digit" + mainKey } else if (/[a-zA-Z]/.test(mainKey)) { mainKey = "key" + mainKey } else { let code = specialCharCodes[mainKey]; if (code) { mainKey = code } } } let ctrl = shortcutKeys.includes("ctrl") let shift = shortcutKeys.includes("shift") let alt = shortcutKeys.includes("alt") let matchShortcut = function (ctrl, shift, alt, mainKey, event) { if (mainKey !== event.code.toLowerCase()) return false if (ctrl !== (event.ctrlKey || event.metaKey)) return false if (shift !== event.shiftKey) return false if (alt !== event.altKey) return false return true }.bind(window, ctrl, shift, alt, mainKey) return matchShortcut } </script> <!-- NOT-PERSISTING EVENT LISTENER --> <script> if (window.ankingEventListeners) { for (const listener of ankingEventListeners) { const type = listener[0] const handler = listener[1] document.removeEventListener(type, handler) } } window.ankingEventListeners = [] window.ankingAddEventListener = function(type, handler) { document.addEventListener(type, handler) window.ankingEventListeners.push([type, handler]) } </script> <!-- CLICKABLE COLORFUL TAGS --> {{#Tags}} <div id="tags-container">{{clickable::Tags}}</div> <script> var tagContainer = document.getElementById("tags-container") if (tagContainer.childElementCount == 0) { var tagList = tagContainer.innerHTML.split(" "); var kbdList = []; var newTagContent = document.createElement("div"); for (var i = 0; i < tagList.length; i++) { var newTag = document.createElement("kbd"); var tag = tagList[i]; // numTagLevelsToShow == 0 means the whole tag should be shown if(numTagLevelsToShow != 0){ tag = tag.split('::').slice(-numTagLevelsToShow).join("::"); } newTag.innerHTML = tag; newTagContent.append(newTag) } tagContainer.innerHTML = newTagContent.innerHTML; tagContainer.style.cursor = "default"; } if (tagContainer.innerHTML.indexOf(tagID) != -1) { tagContainer.style.backgroundColor = "rgba(251,11,11,.15)"; } function showtags() { var tagContainerShortcut = document.getElementById("tags-container"); if (tagContainerShortcut.style.display === "none") { tagContainerShortcut.style.display = "block"; } else { tagContainerShortcut.style.display = "none"; } } var isShortcut = shortcutMatcher(toggleTagsShortcut) ankingAddEventListener('keyup', function (e) { if (isShortcut(e)) { showtags(); } }); </script> {{/Tags}} {{#I0}} {{#Image}} <div id="front-info"> <a href="https://www.ankingmed.com"><img src="_AnKingRound.png"></a> <div>This is an Image Occlusion one by one card: Please flip to the backside.</div> <div class="help"> <p>On the backside, your task is to incrementally reveal an image occlusion. Click on the active rectangle to reveal it and activate the next.</p> </div> </div> <div hidden>{{Image}}</div> {{/Image}} {{/I0}}