8

我有一个动态加载代码的脚本。它是一种搜索引擎。当我按下搜索按钮时,会触发该操作并打开一个包含许多参数的新页面。

我想覆盖在新 URL 中使用脚本生成的参数之一。JS 代码很大而且很难阅读,但我在 Firebug DOM 编辑器中找到了重要的部分。

这是执行搜索时生成的 URL 模式:

http://www.example.com/...?ParameterOne=123&ParameterTwo=Two&ThisParameter=Sth&ParameterFour=Four...

我要编辑的是“ThisParameter”并更改其值。这是在 DOM 中编辑的部分,可以满足我的要求:

Foobar = {
_options: [],
...
var options = {"ParameterOne":123,"ParameterTwo":"Two","ThisParameter":"ABC","ParameterFour":Four,...}
...

当您在 Firebug 的 DOM 选项卡中选择“复制路径”时,这是“ThisParameter”的输出:

_options[0].ThisParameter

我想知道这是可能的。是什么让我认为它是,我可以在 Firebug 中更改此参数并且它可以完美运行。所以,如果 Firebug 可以编辑它,应该有办法用另一个脚本来影响它。

期待任何建议,提前谢谢!

4

3 回答 3

1

由于您无法编辑动态脚本,因此您有以下选项:

  1. 您必须尝试为脚本提供正确的输入,并希望它使用您的值。
  2. 向结果页面添加一个脚本,该脚本将读取 url 和参数,对其进行更改并重定向,正如我们在此处讨论的那样。(如果你把所有东西都放在函数中,如果函数是唯一命名的,它不应该与动态脚本冲突。)

您可以尝试使用搜索按钮在页面中添加类似这样的 jQuery 代码:

$('input[name=search_button_name]').click(function(e) {
    e.preventDefault();
    var form_search = $('#search_form_id');
    $('<input>').attr({
        type: 'hidden',
        name: 'ThisParameter',
        value: 'SomethingElse'
     }).appendTo(form_search);
     f.submit();
});
于 2012-10-23T18:55:13.943 回答
0

You can override any js function and method, or wrap you code around it. The easiest thing would be to look at the code you get and once it gets loaded, you re-declare a method with your own functionality.

I you are trying to replace a parameter in a specific jquery request, you can even wrap around the jquerys ajax method:

var jquery_ajax = $.ajax
$.ajax = function(options){
    // parse only a specific occurence
    if(options.url.indexOf("example.com") > -1) {
           // change the url/params object - depending on where the parameter is
        options.params.ThisParameter = "My Custom value"
    }
    // call the original jquery ajax function
    jquery_ajax(options);
}

But it would be a lot cleaner to override the method that builds the ajax request rather than the ajax request itself.

于 2012-10-31T13:40:18.727 回答
0

我会进一步调查变量选项( var options) 的范围,它是全局的吗?即,如果您在 Firebug 控制台中键入“选项”,它会显示其属性吗?

如果是这样,您可以通过您自己的脚本访问它,并且更改是值,例如

options.ThisParameter = 'my-own-value';

您可以将脚本挂接到搜索按钮的单击事件。

我希望这会有所帮助,如果您在某处有一些示例代码,它可能会更具体。

于 2012-11-01T22:30:03.203 回答