小编nem*_*rod的帖子

如何用Greasemonkey的用户脚本替换JSON字符串中的文本

我想在Firefox中为Greasemonkey创建一个用户脚本而不使用jQuery,它可以在加载网站页面时用新文本替换旧文本.

HTML代码:

..

window.app = profileBuilder({
..
    "page": {
        "btn": {
            "eye": "blue",
            "favorite_color": "blue",
            "gender": "male",
        },
    },
..
});

..
Run Code Online (Sandbox Code Playgroud)

用"红色"代替"喜欢"颜色的"绿色","蓝色",用"女性"代替"男性".

当页面将被加载时,我想看到,例如绿色(不是蓝色)的眼睛和女性性别(不是男性).

我想我需要使用下一个功能:

GM_getValue()
GM_setValue()
JSON.parse()
JSON.stringify()
Run Code Online (Sandbox Code Playgroud)

PS:代码JSON直接在页面中而不在文件中(../code.json)

用户代码:

// ==UserScript==
// @name        nemrod Test
// @namespace   nemrod
// @include     http*://mywebsite.com/*
// @version     1
// ==/UserScript==
var json = {"page": {"btn": {"eye": "blue","favorite_color": "blue","gender": "male",},},};
var stringified = JSON.stringify(json);
stringified = stringified.replace(/"eye": "blue"/gm, '"eye": "green"');
stringified = stringified.replace(/"favorite_color": "blue"/gm, '"favorite_color": "red"');
var jsonObject = JSON.parse(stringified);
Run Code Online (Sandbox Code Playgroud)

它不起作用

有人可以帮助使用正确的代码吗?

html javascript json replace userscripts

9
推荐指数
1
解决办法
5万
查看次数

标签 统计

html ×1

javascript ×1

json ×1

replace ×1

userscripts ×1