posted on August 23

The following code will modify all electronic file links, such as PDF documents, to open using ElectronicFile.aspx instead of DocView.aspx in Laserfiche WebLink 11.x

1. Open Browse.aspx in a text editor, copy the code below and paste it right above the </body> element.

<script>
(function() {

    var proxied = window.XMLHttpRequest.prototype.send;
	var sent = false;
    window.XMLHttpRequest.prototype.send = function() {
		sent = true;
        var pointer = this
        var intervalId = window.setInterval(function(){
			console.log('Ready State: ' + pointer.readyState);
                if(pointer.readyState != 4){
                        return;
                }
                clearInterval(intervalId);
				if(sent)
					transform_links();
				sent = false;			

        }, 1);
        return proxied.apply(this, [].slice.call(arguments));
    };
	
	// After initial load, observe the table that contains the links for any additions due to scrolling
	var t = document.getElementsByClassName('p-datatable-table')[0];
	var observer = new MutationObserver(entryname_column_changes);
	observer.observe(t, {
		attributes: false,
		childList: true, // report added/removed nodes
		subtree: true,   // observe any descendant elements
	});	

})();

function entryname_column_changes(mutations){
  for (let mutation of mutations) {
    if (mutation.type === 'childList') {
      transform_links();
    }
  }
}

function transform_links() {

    //Obtain the EntryNameColumn cells in the browse table and loop through them after a slight delay
    $(".EntryNameColumn").delay(500).each(function () {
		//console.log($(this).text());
        // Find all A elements
		$(this).find("a").each(function() {
			// Get all a elements that have a class value of ElectronicFileIcon
			var ef = $(this).find("div.ElectronicFileIcon");
			// console.log($(this).attr("href"));
			// console.log(ef.length);
			if(ef.length == 1){
				// Get the existing URL
				var old_url = $(this).attr("href");
				// Transform links that have not already been processed
				if(old_url.includes('DocView')){
					// Create a new URL based upon the old one
					var new_url = old_url.replace("DocView","ElectronicFile").replace("?id=","?docid=");
					// Replace old URL with our new one
					$(this).attr("href",new_url);
					// Clone the content which removes all existing event handlers
					var b = $(this).clone();
					// Replace existing content with the clone content which will allow the ElectronicFile link to work.
					$(this).replaceWith(b);
				}
				}
			});
        
    });
};
</script>

2. Save browse.aspx and refresh the page. All of the electronic file links should now open directly.

Hope this helps!

Wesley Funderberg

5 0