开发者

simple Userscript in Greasemonkey no longer working after functions added

开发者 https://www.devze.com 2023-03-31 03:11 出处:网络
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 (f

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');
开发者_如何学Cs.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!


There are multiple problems with that script. The 2 that keep it from running are:

  1. Stray apostrophes as Artyom said.
    Change: '//@name stanfordWhoScraper to // @name stanfordWhoScraper and change: }' to }.

  2. Malformed metadata block:
    Whitespace is required after the leading slashes. This is wrong and fails:

    //==UserScript==
    //@name stanfordWhoScraper
    //@require  http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
    //==/UserScript==
    


    This is correct:

    // ==UserScript==
    // @name stanfordWhoScraper
    // @require  http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
    // ==/UserScript==
    


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消