添加功能后,Greasemonkey 中的简单用户脚本不再工作
我刚刚开始使用 Greasemonkey,并尝试制作一个可以抓取页面的用户脚本 -
在开始之前,我尝试运行一些测试来提高我对 Greasemonkey 的熟悉程度(例如,我尝试了一个只有警报的用户脚本,有效)。但是,在我添加功能后,警报(在顶部调用)无法工作 - 为什么会发生这种情况?
//==UserScript==
'//@name stanfordWhoScraper
//@require http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
//==/UserScript==
alert("TEST");
/*Functions*/
function jquerify(jquerified){
if(!(window.jQuery && window.jQuery.fn.jquery == '1.6.2')) {
var s = document.createElement('script');
s.setAttribute('src', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js');
s.setAttribute('type', 'text/javascript');
document.getElementsByTagName('head')[0].appendChild(s);
jquerified = true;
return jquerified;
}
}
function findName(url){
if (!typeof url) return "Enter string url"
var name = $("#PublicProfile h2").load(url);
if (name == "") return "No name found";
return name;
}'
谢谢!
I've just started to use Greasemonkey and am trying to make a userscript that will scrape a page -
Before I got into that I tried running a few tests to increase my familiarity with Greasemonkey (for example I tried an userscript that had just an alert, which worked). However, after I added functions, the alert (which is called at the top), failed to work- why is this happening?
//==UserScript==
'//@name stanfordWhoScraper
//@require http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
//==/UserScript==
alert("TEST");
/*Functions*/
function jquerify(jquerified){
if(!(window.jQuery && window.jQuery.fn.jquery == '1.6.2')) {
var s = document.createElement('script');
s.setAttribute('src', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js');
s.setAttribute('type', 'text/javascript');
document.getElementsByTagName('head')[0].appendChild(s);
jquerified = true;
return jquerified;
}
}
function findName(url){
if (!typeof url) return "Enter string url"
var name = $("#PublicProfile h2").load(url);
if (name == "") return "No name found";
return name;
}'
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该脚本存在多个问题。阻止它运行的两个因素是:
正如 Artyom 所说的流浪撇号。
将
'//@name stanfordWhoScraper
更改为// @name stanfordWhoScraper
并更改:
}'
更改为}
。格式错误的元数据块:
前导斜杠后需要空格。这是错误的并且失败了:
这是正确的:
There are multiple problems with that script. The 2 that keep it from running are:
Stray apostrophes as Artyom said.
Change:
'//@name stanfordWhoScraper
to// @name stanfordWhoScraper
andchange:
}'
to}
.Malformed metadata block:
Whitespace is required after the leading slashes. This is wrong and fails:
This is correct:
删除第 2 行上的单引号。它启动一个字符串,该字符串在遇到另一个单引号(在函数内部)时结束,因此不会进行解析。
Remove the single quote on line 2. It starts a string that ends when another single quote is encountered (inside your function), and therefore no parsing is made.