This took a while to figure out and then I forgot it all again. I have some Greasemonkey scripts that fiddle around with menus and automatically log me in to a difficult-to-navigate HellishSite. They use jquery and I thought I'd record what to do and pass on the tip.
When I used jquery the old way in my scripts, it broke HellishSite's layout probably because they use jquery in some way themselves. Something about the insane way they use frames might also be the culprit. I didn't look too deep into that. Here's what works. Use a @require line as described in the Metadata block docs for Greasemonkey:
// ==UserScript==
// @name Hello jQuery
// @namespace http://wiki.greasepot.net/examples
// @description jQuery test script
// @include *
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js
// ==/UserScript==
$(document).ready(function() {
$("a").click(function() {
alert("Hello world!");
});
});
The other trick, the bit that was driving me completely crazy, was that in order to get your @require statement to work you have to uninstall the script and install it again. Otherwise, the browser won't read the new info in the metadata block. To do this, you go into the "Manage scripts" control panel for Greasemonkey, edit the script and make sure you copy it somewhere accessible, highlight the script and check "uninstall". Unfortunately there is no "reinstall" button. In your browser address window (on a Mac) point to your copy of the folder you saved it in like this: file:///folder/ and in the browser screen, click on the saved javascript file. It will magically ask you if you want to install it. Yay! Install it and this time it will work. Your other option here is to go edit your Config.xml file where your browser is actually looking to get the metadata when it goes to execute the script.
In the script itself I am doing just a few things.
Testing what page I'm looking at, just by seeing if a particular field name is on that page:
if
($("input[name^='lastName']"))
{
(do some stuff)
}
Filling out fields:
$("input[name='loginId']").val("Sucky");
$("input[name='password']").val("blowme");
(the non-jquery way to do this is something like,
document.getElementsByName('password')[0].value = "blowme"; Ugh, shudder...)Selecting from dropdown menus:
$("select[name='profile']").val("sucktastic");
Pre-selecting a bajillion clicky things from horrible little fields full of options:
$("option[value='8008135']").attr("selected","Selected");
Making horrible little fields bigger, that show 4 options at a time out of possible 25:
$("select[name='section']").attr("size",25);
All this will become unnecessary once I dive into the HellishSite API but right now I don't have time to go that route.
A note that I wrote this stuff back in March, adn then it broke, I fixed it, it broke again, I had forgotten it all and had to re-learn it, then my hard drive crashed and I lost my fixes and have spent a while fixing it again. This is pretty typical for me. I learn enough to make what I need, but I don't go out and become a Greasemonkey or javascript expert. Just enough to get the job done and move on.




1 comment:
好秘书 中国呼吸网 肿瘤网 中国皮肤网 癌症康复网 中国公文网 工作总结 个人工作总结 班主任工作总结 年终工作总结 工作报告 政府报告 述职报告 述廉报告 考察报告 自查报告 情况报告 调研报告 调查报告 申请报告 辞职报告 实习报告 评估报告 工作汇报 思想汇报 汇报材料 情况通报 情况汇报 心得体会 学习心得 工作心得 培训心得 读后感 演讲稿 竞聘演讲 就职演讲 比赛演讲 征文演讲 节日演讲 演讲技巧 工作意见 活动策划 工作方案 整改方案 实施方案 企划文案 销售方案 培训方案 应急预案 规章制度 法律法规 事迹材料 先进事迹 个人事迹 申报材料 学习材料 考察材料 经验材料 交流材料 自我鉴定 模板范例 技巧经验 工作计划 民主生活会 入党志愿书 入党申请书 入团申请书 转正申请书 通知 毕业论文 合同
Post a Comment