A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Showing posts with label jquery. Show all posts
Showing posts with label jquery. Show all posts

Add Javascript to a security protected page

If you want to run some JQuery (or any other script) on a page that doesn't have it already, you can load it like this in the web browser console:

var script = document.createElement('script');
script.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(script);

But on pages with a strict security policy, you may get an error:

Refused to load the script '......' because it violates the following Content Security Policy directive: .....

Solution: Edit the DOM and replace the security policy with this one:

<meta http-equiv="Content-Security-Policy" content="default-src * self blob: data: gap:; style-src * self 'unsafe-inline' blob: data: gap:; script-src * 'self' 'unsafe-eval' 'unsafe-inline' blob: data: gap:; object-src * 'self' blob: data: gap:; img-src * self 'unsafe-inline' blob: data: gap:; connect-src self * 'unsafe-inline' blob: data: gap:; frame-src * self blob: data: gap:;">

(source)

Breakdown of policy from that guy:
  • default-src * self                 blob: data: gap:;
  • style-src   * self 'unsafe-inline' blob: data: gap:;
  • script-src  * 'self' 'unsafe-eval' 'unsafe-inline' blob: data: gap:;
  • object-src  * 'self'               blob: data: gap:;
  • img-src     * self 'unsafe-inline' blob: data: gap:;
  • connect-src self * 'unsafe-inline' blob: data: gap:;
  • frame-src   * self                 blob: data: gap:;
Hmm, that doesn't look quite right. Seems inconsistent.

Stop Confluence making CamelCase links

You can include the following code in the page, or make a macro out of it.
Note: You need to attach JQuery to one of your wiki pages, and edit the URL in the code.
{html}
<script type="text/javascript" src="/download/attachments/59835554/jquery-1.2.6.js"></script>
<script type="text/javascript"> 
//The $ notation is used by DWR. So we rely on jQuery namespace only
jQuery.noConflict();          
jQuery(document).ready(function() {
   //The camel case links have class createlink. So fetch all such links and remove the html part and retain the text part
   jQuery("a.createlink").each(function(index){
    var text = jQuery(this).text();
    if(isValidCamelCase(text)){//Only filter where we have a camelcase text and not valid yet to be created link
       jQuery(this).after(text).remove();
    }
    return true;
   });
});

function isValidCamelCase(s){
 if(s == null || s.length == 1){
  return false;
 }
 
 if(s.indexOf(' ') != -1){
  return false;
 }

 //Check for case where valid link is being created like [Test123]
 var indexOfNonUpperCaseChar = -1;
 for(var i = 0; i < s.length; i++){
  var c = s.charAt(i);
  if(!isUpperCase(c)){
   indexOfNonUpperCaseChar = i;
   break;
  }
 }
 
 if(indexOfNonUpperCaseChar == -1){
    return false;
 }
 
 var lowerCase = s.toLowerCase().substring(indexOfNonUpperCaseChar);
    var originalCase = s.substring(indexOfNonUpperCaseChar);
    if(originalCase == lowerCase){
     return false;
    }
 return true;
}

function isUpperCase(char){
 return char >= 'A' && char <= 'Z'
}
</script>
{html}

Thanks