74 lines
2.5 KiB
JavaScript
74 lines
2.5 KiB
JavaScript
/*******************************************************************
|
|
|
|
Module : /assets/js/share.js
|
|
Desc. : JS Class Share File Cross Browser
|
|
Created By : Siau Pheng ([email protected]).
|
|
Created Date : May 27th, 2007.
|
|
Last Modified : January 19th, 2026.
|
|
|
|
(c) 3FONIA Software; WWW.3FONIA.COM.
|
|
|
|
*******************************************************************/
|
|
|
|
var cSHARE = function(url, options) {
|
|
var _options = {
|
|
'url': '',
|
|
'filename': '',
|
|
'title': '',
|
|
'text': 'share dari aplikasi'
|
|
};
|
|
var _params = [];
|
|
this.construct = function(url, options){
|
|
if (typeof url == 'object') {
|
|
$.extend(_options, url);
|
|
} else {
|
|
$.extend(_options, options);
|
|
_options.url = url;
|
|
}
|
|
};
|
|
this.set_url = function (url){
|
|
_options.url = url;
|
|
};
|
|
this.add_param = function(par_id, par_val) {
|
|
var _getvalue = (par_val) ? par_id + '=' + escape(par_val) : par_id;
|
|
_params.push(_getvalue);
|
|
};
|
|
this.share = async function() {
|
|
if (!_options.url) {
|
|
console.warn('cek url !');
|
|
return false;
|
|
}
|
|
var _pro = (window.location.protocol=='https:') ? 'https://' : 'http://';
|
|
var _src = _pro+FoniaJS.WEB_HOST + _options.url + '?' + $.param(FoniaJS.RequestParam(_params));
|
|
|
|
try {
|
|
const response = await fetch(_src)
|
|
.then(response => {
|
|
const cDispo = response.headers.get('Content-Disposition');
|
|
if (cDispo && cDispo.indexOf('filename=') !== -1) {
|
|
_options.filename = cDispo.split('filename=')[1].split(';')[0].replace(/"/g, '');
|
|
}
|
|
return response;
|
|
});
|
|
const blob = await response.blob();
|
|
const file = new File([blob], _options.filename, { type: blob.type });
|
|
|
|
if (navigator.canShare && navigator.canShare({ files: [file] })) {
|
|
await navigator.share({
|
|
files: [file],
|
|
title: _options.title ?? _options.filename,
|
|
text: _options.text
|
|
});
|
|
} else {
|
|
const link = document.createElement('a');
|
|
link.href = URL.createObjectURL(blob);
|
|
link.download = _options.filename;
|
|
link.click();
|
|
}
|
|
} catch (error) {
|
|
console.error('Sharing gagal:', error);
|
|
}
|
|
};
|
|
this.construct(url, options);
|
|
};
|