【发布时间】:2015-03-31 13:55:35
【问题描述】:
我想在不使用jQuery的情况下在Firefox中为Greasemonkey创建一个用户脚本,它可以在加载网站页面时用新文本替换旧文本。
HTML 代码:
..
window.app = profileBuilder({
..
"page": {
"btn": {
"eye": "blue",
"favorite_color": "blue",
"gender": "male",
},
},
..
});
..
用“绿色”代替眼睛的“蓝色”,用“红色”代替喜欢颜色的“蓝色”,用“女性”代替“男性”。
当页面加载时,我想看到,例如绿色(不是蓝色)代表眼睛,女性代表性别(不是男性)。
我想我接下来需要使用函数:
GM_getValue()
GM_setValue()
JSON.parse()
JSON.stringify()
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);
没用
有人可以帮助编写正确的代码吗?
【问题讨论】:
-
这部分是你的脚本还是页面本身?
-
您尝试过什么?为什么您猜测您需要使用这些功能?
-
@vishalsharma 这部分在页面上。
-
@paul 因为我在网上看了?
-
这只是一个对象,所以你可以使用 unsafeWindow.page.btn.eye = "green";
标签: javascript html json replace userscripts