Hi , I am putting javascript code in a String variable via a plugin class that would install the custom field. When I put text in the variable it shows up fine in the description of the field in JIRA , but when its javascript code, it does not show up at all.
I took care to properly format the js code (adding escape chars where neccessary) but still it doesnt show up.
Is it possible to add javascript code as description for a custom field we are creating and have you done it before (via a plugin) ?
Community moderators have prevented the ability to post new answers.
I'm also bitten regularly during upgrades by bits of JavaScript in field descriptions. You start with it in one place, then it gets copied and modified to other fields, none of it version controlled in any way. You change the JIRA release and suddenly you have 25 different places in JIRA you have to fix scripts. The Behaviours plugin is a better solution in general.
I'll repeat my comment from https://answers.atlassian.com/questions/286401/how-can-i-put-javascript-code-to-spice-up-a-multi-select-custom-field-i-am-creating-in-a-pluginhere
Jira allows setting js code in the description. I think you do not need to escape it. The script will not show up in the description but it's there. You can do a simple test by putting <script>console.log('test message')</script> and check if it will show up in the browser console.
https://confluence.atlassian.com/display/JIRA/Fields+Allowing+Custom+HTML+or+JavaScript
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Boris the reason I need escape sequence is as follows:
String jsCode = "<script type=\"text/javascript\">....." so on
Also the link you gave seems to be about inserting javascript code to the field via JIRA (running instance). I am talking about setting the description with the js from code (via a plugin).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It is the same actually. Can you provide the code you're currently using ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sure. See below:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@jamieif you could provide the following comment:
" I would create a new CF type with a different template, and then have a js resource that looks for these cf types on page load and manipulates them. Bit more hassle, but could be worth it in the end."
as an answer I would love to mark it as correct answer as its a good point of view of yours.
Thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
//create a list of issue types for which the screen needs to be available List<GenericValue> issueTypes = new ArrayList<GenericValue>(); issueTypes.add(null); //create a list of project contexts for which the custom field needs to be available List<JiraContextNode> contexts = new ArrayList<JiraContextNode>(); contexts.add(GlobalIssueContext.getInstance()); //get params to create custom field String customFieldTypeKey = "com.atlassian.jira.plugin.system.customfieldtypes:multiselect"; String customFieldSearcherKey = "com.atlassian.jira.plugin.system.customfieldtypes:multiselectsearcher"; CustomFieldSearcher customFieldSearcher = customFieldManager.getCustomFieldSearcher(customFieldSearcherKey); //create custom field CustomField cField = customFieldManager.createCustomField(customfieldName, "", customFieldManager.getCustomFieldType(customFieldTypeKey) , customFieldSearcher, contexts, issueTypes); StringBuilder jsCode = new StringBuilder("<script type=\"text//javascript\">"); //remove none option from multiselect field
jsCode.append("AJS.$(\"#" + cField.getId() + " option[value='-1']\").remove();"); //add javascript code to make the multiselect field look fancy like the components multiselect field jsCode.append("(function($)"); jsCode.append("{"); jsCode.append("new AJS.MultiSelect("); jsCode.append("{"); jsCode.append("element: $(\"#" + cField.getId() + "\"), itemAttrDisplayed: \"label\", errorMessage: AJS.params.multiselectComponentsError"); jsCode.append("});"); jsCode.append("}"); jsCode.append(")(AJS.$);"); jsCode.append("<//script>"); cField.setDescription(jsCode.toString()); customFieldManager.updateCustomField(cField);
//add multiselect custom field to default screen FieldScreen defaultScreen = fieldScreenManager.getFieldScreen(FieldScreen.DEFAULT_SCREEN_ID); if(!defaultScreen.containsField(cField.getId())) { FieldScreenTab firstTab = defaultScreen.getTab(0); firstTab.addFieldScreenLayoutItem(cField.getId()); }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Why are you putting '//' in the close html tags ? "<//script>" ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Boris, you are totally correct.
Costly oversight.
Going to accept your answer in the context of this conservation.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ouch... good spot Boris.
For me embedding code in strings is a bad smell... I pity the person that tries to maintain this. FWIW, I would create a new CF type with a different template, and then have a js resource that looks for these cf types on page load and manipulates them. Bit more hassle, but could be worth it in the end.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Jamie actually you are correct from a maintenance point of view in the long term it is not ideal. However I just wanted to have a multiselect field which looks like the components field. And the javascript is there just to make an ordinary multiselect field look like the components field.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Jamie, I agree with you, but the original question was around injecting JS code into the description, so we drilled down on that topic without thinking about the potential problems :).
Umiar, I advice you to consider Jamie's suggestion, to avoid future headaches with that code.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Noted. However, as you mentioned I was merely curious if its possible to inject js that way via a plugin and as to why it wasnt working (if it was allowed).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Boris, sure, it wasn't meant as a criticism. I was just offering my experience, unasked ;-)
Like many people I have been bitten by having javascript in velocity templates.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Jamie, I'm always happy to hear your opinion as we all really can learn a lot from you.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Boris and @jamie, what if i were to put the js code a .js file embedded with the plugin and have the plugin class just read the .js file from there.
In this way the code is easily readable and can be easily modified.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Community moderators have prevented the ability to post new answers.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.