问题描述
我试图从Bigcartel覆盖JS函数。我无法访问JS文件。
Im trying to override a JS function from Bigcartel. I have no access to the JS file.
原文是:
updateCart: function(cart) {
$('aside .cart .count, .main header .cart').htmlHighlight(cart.item_count);
return $('aside .cart .total').htmlHighlight(Format.money(cart.total, true, true));
},
我正在尝试将其更改为:
And i am trying to change it to this:
updateCart: function(cart) {
$('aside .cart .count, .sml .cart, .big .cart .count').htmlHighlight(cart.item_count);
return $('aside .cart .total').htmlHighlight(Format.money(cart.total, true, true));
},
我知道其他人也问了类似的问题,但我是一个完整的当谈到理解如何实现JS(我只知道如何通过试验和错误调整)时,noob
I am aware that others have asked similar questions, but i am a complete noob when it comes to understanding how to implement JS (i only know how to tweek through trial and error)
如果有人可以这样帮助我给我答案会很棒。
If any one could be so kind as to help me out by giving me the answer that would be great.
谢谢,
iWed-
编辑[10.10.13 :: 21:24hr]
为了澄清,我没有直接访问原始JS文件。我只能通过chrome查看它。我只能访问html文件。这是一个大卡特尔主题编辑。
To clarify, i do not have direct access to the original JS file. i can only view it through chrome. I only have access to html files. It is for a Big cartel theme Edit.
这是使用chrome复制JS的链接。
第216行是代码,如果这有帮助:
Here is a link to to copied JS using chrome.Line 216 is the code, if this helps : http://jsfiddle.net/w9GTJ/
推荐答案
编辑:你很幸运。从发布的代码中可以看到updateCart方法是在window.Store全局对象上导出的。解决方案是在加载原始脚本后添加此代码:
You are in luck. From the posted code you can see that the updateCart method is exported on the window.Store global object. The solution is to add this code after the original script loaded:
window.Store.updateCart = function(cart) {
$('aside .cart .count, .sml .cart, .big .cart .count').htmlHighlight(cart.item_count);
return $('aside .cart .total').htmlHighlight(Format.money(cart.total, true, true));
};
一般情况说明:
网页中加载的所有脚本都在同一个全局范围内运行,因此覆盖变量就像之后插入脚本一样简单:
All scripts loaded in a web page run in the same global scope, so overwriting a variable is as simple as inserting your script afterwards:
<script>
var x = 5; // original script
</script>
<script>
x = 2; // your inserted script
</script>
从它的外观来看,你的函数被定义为一个对象的属性:
From the looks of it, your function is defined as property of an object:
var x = {
updateCart : function(cart) {
// stuff
}
}
所以要覆盖它你需要做:
So to overwrite it you need to do:
x.updateCart = function(cart) {
// your code
}
最后,如果函数在原始代码中是私有的,有一种情况你根本无法覆盖它:
Finally, there is one situation where you simply can't overwrite it, if function is private in the original code:
function() {
var x = {
updateCart: function(){}
}
}()
// No way to access x.updateCart here
这篇关于从另一个文件覆盖JS函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!