You are viewing limited content. For full access, please sign in.

Discussion

Discussion

HOWTO: Force all electronic files to open using ElectronicFile.aspx

posted on August 23, 2023

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

6 0
replied on February 22 Show version history

Hi

Thanks for sharing.

This works great when browsing the the Repository and Clicking on an Entry if I click a PDF it will automatically convert to ElectronicFile.aspx.

It does not seem to work when passing a URL Link. It does not automatically detect an Electronic Document, use ElectronicFile.aspx and download.

If I pass a URL it does not automatically change to ElectronicFile.aspx.

The idea being docview.aspx is used from a Web application which might be a Laserfiche Standard, PDF or Excel. WebLink 9 just automatically detects and handles when using below to automatically download the file.

https://test.ca/weblinktest/docview.aspx?dbid=0&id=888475&openfile=true

0 0
replied on February 22

William,

 You are correct about it not working if a URL is passed to Laserfiche as it's only manipulating the links on the client side. After WebLink 9, Laserfiche made major changes to the back-end which forced all documents to go through DocView.aspx.

 

0 0
replied on February 23

Thanks so much for your post and subsequent follow up.

The confirmation helps a great deal.

0 0
replied on February 1

I implemented this in our test bed and it did indeed download the file with just a click but did not prompt me to open it. I had to click on it in the download folder to open it.

I think part of the problem might be that the file does not have an extension which is why I'm looking into something like this in the first place.

If I could get it to give the user the 'How do you want to open this file?' prompt with a click on the link, I would be thrilled.

0 0
replied on February 22

Michael,

 That is browser independent on how it handles files that a user clicks on.

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.