将新的简单javascript注入magento后端(作为模块) [英] Inject new simple javascript to magento backend (as a module)
问题描述
我想在magento产品管理后端的一些描述和元字段上添加简单的字符计数器功能。就像下面的截图一样。
I want to add simple char counter functionality on some of the description and meta field on magento product administration backend. Just like on this below screenshot.
我通过将简单的原型脚本添加到一个加载的JS文件中来做到这一点magento admin HTML。我选择了browser.js( /js/mage/adminhtml/browser.js
),因为那是在我的magento安装的后端区域加载的最后一个脚本。这是我的原型脚本代码块:
I did that by adding simple prototype script into one of the JS file which get loaded on magento admin HTML. I choose browser.js (/js/mage/adminhtml/browser.js
) for that because that is the last script which get loaded on the backend area of my magento installation. This is my prototype script chunk of code:
/* ADMIN CHAR COUNTER SCRIPT */
Event.observe(window, 'load', function() {
Element.insert( $('meta_title').up().next().down('span'), {
'after': "<div id='meta_title_counter'>Char count: <span id='meta_title_counter_num'>"+(69-$('meta_title').getValue().length)+"</span></div>"
});
Element.insert( $('meta_description').up().next().down('span'), {
'after': "<div id='meta_description_counter'>Char count: <span id='meta_description_counter_num'>"+(156-$('meta_description').getValue().length)+"</span></div>"
});
Element.insert( $('short_description').up().next().down('span'), {
'after': "<div id='short_description_counter'>Char count: <span id='short_description_counter_num'>"+$('short_description').getValue().length+"</span></div>"
});
Element.insert( $('description').up().next().down('span'), {
'after': "<div id='description_counter'>Char count: <span id='description_counter_num'>"+$('description').getValue().length+"</span></div>"
});
Event.observe('meta_title', 'keyup', function(event) {
$counter = 69-$('meta_title').getValue().length;
$("meta_title_counter_num").update($counter);
if($counter < 0){ $("meta_title_counter").setStyle({ color: 'red' }); }
else{ $("meta_title_counter").setStyle({ color: '#6F8992' }); }
});
Event.observe('meta_description', 'keyup', function(event) {
$counter = 156-this.getValue().length;
$("meta_description_counter_num").update($counter);
if($counter < 0){ $("meta_description_counter").setStyle({ color: 'red' }); }
else{ $("meta_description_counter").setStyle({ color: '#6F8992' }); }
});
Event.observe('short_description', 'keyup', function(event) { $("short_description_counter_num").update(this.getValue().length); });
Event.observe('description', 'keyup', function(event) { $("description_counter_num").update(this.getValue().length); });
});
/* END OF CHAR COUNTER MODULE */
我确实意识到我的意思完成是一个如此快速和肮脏的技巧。我几乎编辑核心文件。这意味着在升级magento后将删除此脚本。我的老板让我把这个功能放到一个模块中。但我没有任何创建magento模块的经验。我试图找到一些关于如何创建简单的magento模块的基础教程。但是这些教程都没有给我一种注入新脚本的方法。这可能是最近的指南:
I do realize that what I've done is such a quick and dirty trick. I practically edit the core file. That means that this script will removed after an upgrade of magento. My boss ask me to put this functionality into a module. But I don't have any experience on creating magento module. I've tried to find some basic tutorial on how to create simple magento module. But none of those tutorial gives me a way to inject new script. This one may be the closest guide:
但我仍然不知道从哪里开始创建这个简单的模块。我很抱歉,如果这个问题感觉太新手了,但我真的需要帮助,而不像平常一样,这次谷歌无法帮助我(或者至少我找不到一个好的关键词来开始谷歌搜索)。所以在这里,我希望那里有人会很乐意帮助我:)。
but I still don't have any idea at all where to start to begin this simple module creation. I'm sorry if this question feels too newbie, but I really need help here and unlike the usual, this time Google can't help me out (or at least I can't find a good keyword to start googling). So here I am hoping that somebody out there would be glad to help me :)
推荐答案
尝试使用管理员布局模块文件更新添加
Try in your module file with admin layout updates add
<adminhtml_catalog_product_edit>
<reference name="head">
<action method="addJs"><script>your_js_file.js</script></action>
</reference>
</adminhtml_catalog_product_edit>
甚至
<default>
<reference name="head">
<action method="addJs"><script>your_js_file.js</script></action>
</reference>
</default>
在所有管理页面上添加你的js文件。
to add load your js file on all admin pages.
这篇关于将新的简单javascript注入magento后端(作为模块)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!