In a testing environment, my addon works (using atlas-run on localhost). In a vm file I have my function and then I call it with:
window.onload = function() {
changeCSS();
};
But after I package it and upload it to my production server of the software, it stops working. I ran the function with js while inspecting the page, so i know the function is there and works. So it seems that windows.onload isn’t working.
You can try to disable the compression by adding <compressResources>false</compressResources> to the configuration section of the maven-jira-plugin in your pom.xml. Package your add-on and install it in your production environment, if the problem is resolved you will know it is caused by the compression / bundling.
If you’re using jQuery, you can also rely on their logic to ensure the code snippet is executed when the window/document is ready for manipulation:
$(function() {
changeCSS();
bulkOff();
});
In addition, you might want to add a console.log('Hi!'); to the window.onload function to see if your code is executed. If it is, you should see ‘hi’ in the developer/javascript console.
This worked. but it’s not working for my function changeCSS(). What i’m changing is css on a kanban board. Not sure why it’s not triggering for this specific instance.
I used console.log(‘Hi!’); to try and figure out where it’s breaking. It looks like my function is being called before the page finished loading the kanban board. Is there a way to delay it until it’s fully loaded?
@kjacobs ah yes, many features of the host products are actually loaded asynchronously. As such, executing code on load may get you stuck in a race condition.
If you want to resolve this, you will have to determine which element you require to be available and create logic to wait for this element to be available (I haven’t tested this code, I’m writing it on the fly):
function waitForElement(selector, callback) {
var count = 0;
var interval = window.setInterval(function() {
count++;
if (count > 10) {
callback(new Error('timeout'));
} else if (document.querySelectorAll(selector).length > 0) {
window.clearInterval(interval);
callback();
}
}, 500);
}
waitForElement('#myElementId', function (err) {
if (!err) {
console.log('the element is available!');
} else {
console.log('the element is NOT available after 5 seconds');
}
});