您好,
我尝试编写一个脚本来获取个人资料标题的 background-image 属性,然后将其保存为根变量。但是,我的代码不起作用。我尝试了许多解决方案,其中一些包括 AI,但它们只是还原了插件版本,并没有提供帮助。
这是代码:
<script type="text/discourse-plugin" version="0.9">
const setRootBgImage = () => {
try {
const el = document.getElementByClassName('.user-profile-image');
if (el) {
const bgImage = window.getComputedStyle(el).getPropertyValue('background-image');
document.documentElement.style.setProperty('--profile-bg-img', bgImage);
}
} catch (error) {
console.error(error);
debugger; //运行时,无论如何都会出现此问题,并显示相同的错误
}
};
const shouldRun = () => {
return window.location.pathname.startsWith('/u/');
};
const init = () => {
if (shouldRun()) {
setRootBgImage();
}
};
document.addEventListener('DOMContentLoaded', init);
api.onPageChange(() => {
init();
});
</script>
谢谢。