I want to get the value of a certain field of a certain card. Where is the mistake in this writing?

有懂anki插件编写的吗?我想得到 某张卡片的”英式音标“字段的值:我在 _init_.py 中写了一个简单的代码,插件是成功的,但在卡片中调用时,总是错误。

import anki

def on_get_field_value(note_id, field_name):
    """Return the value of the specified field on the given note."""
    note = anki.notes.Note(note_id)
    return note.fields[field_name]

anki.hooks.addHook("get_field_value", on_get_field_value)

卡片中调用如下:

<script>
//返回卡片字段的值
var fieldValue = anki.get_field_value("1699282826487","英式音标");
alert(fieldValue)
</script>

通过以上处理,插件顺利导入了,得不到字段的值,哪里错了?

1 Like

It looks like you are trying to call a Python function from JS code on the card, which doesn’t work.

You may be able to use an Anki addon such as Anki Connect to fetch this data.

1 Like

我每张卡片第一次点击按钮时遇到了错误提示:not at top of queue

<script>
var $buttonReset = $("<button id='myButtonReset'>已学</button>");
$("#en").append($buttonReset);


$buttonReset.click(function(){
	toOne(1694413136146);	
});


async function toOne(cidd){
	try {
			var result = await invoke("answerCards", 6, {"answers":[{"cardId":cidd,"ease":1}]});
			alert(`got list of decks: ${result}`);
	} catch (error) {
        alert(error);
  }
}

//这是Anki connect 网站提供的 javascript 方法
function invoke(action, version, params={}) {
    return new Promise((resolve, reject) => {
        var xhr = new XMLHttpRequest();
        xhr.addEventListener('error', () => reject('failed to issue request'));
        xhr.addEventListener('load', () => {
            try {
                var response = JSON.parse(xhr.responseText);
                if (Object.getOwnPropertyNames(response).length != 2) {
                    throw 'response has an unexpected number of fields';
                }
                if (!response.hasOwnProperty('error')) {
                    throw 'response is missing required error field';
                }
                if (!response.hasOwnProperty('result')) {
                    throw 'response is missing required result field';
                }
                if (response.error) {
                    throw response.error;
                }
                resolve(response.result);
            } catch (e) {
                reject(e);
            }
        });

        xhr.open('POST', 'http://127.0.0.1:8765');
        xhr.send(JSON.stringify({action, version, params}));
    });
}
</script>