How to user jQuery from a greasemonkey script?

Richard Cullen February 20, 2013

Hi there

I'm trying to create a greasemonkey script to tweak the Jira UI. I want to use jQuery but when I do I get:{code}

Uncaught ReferenceError: $ is not defined

{/code}

So I change it to reference AJS and get:

{code}

Uncaught ReferenceError: AJS is not defined

{/code}

The script is pretty simple so far:

{code}

document.body.onload = function(){

AJS.$('.link-content').removeClass('collapsed-link');

AJS.$('#show-more-links').addClass('collapsed-link');

};

{/code}

Any ideas?

Thanks

2 answers

0 votes
OsmanA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 20, 2013

Hey Richard,

So content scripts run in an isolated environment:

https://developer.chrome.com/extensions/content_scripts.html#execution-environment

Which essentially means you can't access the jquery/existing javascript without a little work.

So to access the existing jquery in the page you have several methods. Chrome doesn't support @require so you can't use that to reference the jquery.js if using chrome. Best way is prob to inject :

function scriptLoader(callback) {

var script = document.createElement("script");

script.textContent = "(" + callback.toString() + ")();";

document.body.appendChild(script);

}

function mainCode() {

}

scriptLoader(mainCode);

I probably should have said - this isn't a JIRA specific issue its just the way userscripts interact with the browser.

0 votes
TylerA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 21, 2013

Richard,

Per this other answers post, it sounds like the AtlassianUI pieces aren't loading correctly. Are you able to get this working via a local (development) copy of JIRA as would be generated by the Plugin SDK? If you can get it working correctly against a download copy of JIRA, then perhaps you can get it working against OnDemand with a bit of tweaking?

Does that help?

Richard Cullen February 26, 2013

Sorry Tyler, don't have the time to download, install, configure & debug a local version. Given that we're just using the default OnDemand instance I guess it shouldn't be hard for you to repro?

Suggest an answer

Log in or Sign up to answer