-1

Even though I have my userscript restricted to one domain, any site I visit that uses Jquery experiences all kinds of nasty issues when my script is active. Checking the error console in chrome reveals an identical error on all sites:

"Uncaught TypeError: Property '$' of object [object Window]"

What's causing this? My objective is to get my userscript running in noconflict mode on a site that uses both jquery and prototype. I didn't make the code above var = myFunction, so I don't know what about it is causing the problem I'm running into. Any suggestions?

// ==UserScript==
// @name          Restore Dashboard Tags
// @namespace     http://userstyles.org
// @description   This script restores a user's tracked tag list to the sidebar on tumblr
// @author       
// @homepage     
// @history       1.0 first version
// @include http://www.tumblr.com/*
// @match   http://www.tumblr.com/*

// ==/UserScript==

var jQuery, $ = null;

function addJQuery(callback) {
var p = null;

if(window.opera || window.navigator.vendor.match(/Google/)) {
    var div = document.createElement("div");
    div.setAttribute("onclick", "return window;");
    p = div.onclick();
}
else {
    p = Window;
}

jQuery = $ = p.jQuery.noConflict();
callback();
}

var myFunction = function() {
jQuery('div#right_column ul:first-child').after('<ul class="controls_section" id="tracked_tags"></ul>');
jQuery('div.tracked_tags a').each(function (i) {
    var tagID = jQuery(this).attr("id");
    var tagIDNumber = tagID.replace('tag_','');
    var tagName = jQuery(this).attr("href");
    var tagNameClean = tagName.replace('/tagged/','');
    var tagContent ='';
    tagContent += '<li><a href="'+tagName+'" id="'+tagID+'" class="tag">';
    tagContent += '<div class="hide_overflow">'+tagNameClean+'</div>';
    tagContent += '<span id="tag_unread_'+tagIDNumber+'" class="count" style=""></span></a></li>';                  
    jQuery(tagContent).appendTo('div#right_column ul#tracked_tags');                                                                      
}); 
};

var NewPosts = function(){
    jQuery('div.tracked_tags > div').each(function (i) {
        var thisIndex = jQuery(this).index();
        if (jQuery(this).find('small').length){         
            var postCount = jQuery(this).find('small').text();      
            jQuery('div#right_column ul#tracked_tags li:eq('+thisIndex+')').find('.count').html(postCount.replace("new posts", "") );
        }
    });
                setTimeout(NewPosts,30000);
}


addJQuery(myFunction);
addJQuery(NewPosts);
ajw-art
  • 173
  • 1
  • 1
  • 12
  • What browser/software are you using, Greasemonkey? Opera? – NoBugs Oct 04 '12 at 05:13
  • @NoBugs Chrome. I just installed the script by dragging it into the extensions window. – ajw-art Oct 04 '12 at 05:37
  • It might be a problem with the @include, see http://www.chromium.org/developers/design-documents/user-scripts – NoBugs Oct 04 '12 at 06:00
  • No effect, sadly. I keep getting the "Uncaught TypeError: Property '$' of object [object Window] is not a function" error. – ajw-art Oct 04 '12 at 06:21
  • @NoBugs The problem has been solved! Someone on another site IDed the culprit as `jQuery = $ = p.jQuery.noConflict();`; since I wasn't loading my own copy of Jquery I didn't need noConflict, and its usage was hiding Jquery from the rest of the page. – ajw-art Oct 04 '12 at 06:29

1 Answers1

0

The problem has been solved! Someone on another site IDed the culprit as jQuery = $ = p.jQuery.noConflict();; since I wasn't loading my own copy of Jquery I didn't need noConflict, and its usage was hiding Jquery from the rest of the page.

ajw-art
  • 173
  • 1
  • 1
  • 12
  • That is not the (only) problem. It certainly doesn't explain how the script breaks "any site I visit that uses Jquery". – Brock Adams Oct 04 '12 at 06:50
  • @BrockAdams It's working perfectly for me on Chrome at least; no issues with other sites now. I'll test it on Firefox in the morning. – ajw-art Oct 04 '12 at 07:06
  • I'm glad your script appears to work for you. But, this answer does not explain the issue stated in the question. – Brock Adams Oct 04 '12 at 07:26