我想修改作为网页一部分下载的Javascript。
我有这个页面: https://discussions.apple.com/thread/7629335
我想修改函数jspaginate.init。我已经走到了这一步:
console.log(window.jspaginate)
Object { data: Object, loading: false, init: jspaginate.init(), update: jspaginate.update(), pushState: jspaginate.pushState(), loadingSequence: jspaginate.loadingSequence(), removeLoading: jspaginate.removeLoading(), updateUI: jspaginate.updateUI(), getData: jspaginate.getData() }
undefined
console.log(window.jspaginate["init"])
function jspaginate.init()
console.log(window.jspaginate["init"].toString())
function (action, last){
var view = this,
target, current;
... clipped ...
背景: 这个页面包含很多javascript。从站点服务器下载jspaginate函数。不过,我需要更改函数jspaginate。我无法访问服务器。我想更改我的jspaginate副本。我知道每次下载都需要更改功能。
我使用GreaseMonkey插入一些javascript。
答案 0 :(得分:1)
只需使用新定义覆盖init函数,如下所示:
window.jspaginate.init = function() {
console.log('there you go');
}
答案 1 :(得分:-1)
这是我使用的代码。由于我已将jspaginate.init转换为字符串并修改了字符串,因此我必须将字符串转换为函数。我通过eval函数进行了转换。
var debug = 1;
/* Get the string of the function to change */
var stringed = jspaginate["init"].toString();
if ( debug ) console.log ("--> cloneGotoChange: \n" + stringed);
/* Place the added code as the last statement in the function jspaginate["init"]
Find the function closing } */
position = stringed.lastIndexOf("}");
/* Change the downloaded javascript */
var newCode = stringed.substr(0, position)
+ " console.log (\"--> before \" ); cloneGotoUpate(); console.log (\"--> after \" ); "
+ stringed.substr(position);
/* Inject the changed code
need to use the eval function to make a function assignment instead of a string assignment */
eval( 'jspaginate["init"] =' + newCode);
if ( debug ) console.log (console.log(window.jspaginate["init"].toString()));