DataviewJs - common place for scripts on mobile

In continuation of the topic

DataviewJs - Code reuse: common place for scripts

The solution listed there works only on PC, not on Mobile (Android). The path to the file is correct, but the DataviewJs script cannot find the file with the script.

Minimal Working Example:

```dataviewjs
let utilPath = app.vault.adapter.basePath + "/myscripts/myDataviewUtils.js"

function checkFileExistsSync(filepath){
  let flag = true;
  try{
  	if (fs.existsSync(utilPath)) {
    //file exists
  	}
  }catch(e){
    flag = false;
  }
  return flag;
}

dv.paragraph("checkFileExistsSync: " + checkFileExistsSync(utilPath) + "<br>")
dv.paragraph("---------------------")
var myUtils = require(utilPath);
dv.paragraph("utilPath: " + utilPath)
dv.paragraph(myUtils.f1())

File 'myDataviewUtils.js':

export function f1() {
console.log(“Hello from f1”);
return “Hello from f1”
}


Does anyone know how I can call external DataviewJS functions?
I need the functions in many places and am currently copying all the code back and forth. The goal is to only specify the function call. Changes would then automatically affect all files.
1 Like

I can confirm the strategy to export functions and then require them inside a dataviewjs block blows up on mobile. Really hoping we can figure out why it behaves differently on mobile than on desktop.

4 Likes

Hello! I’m back with a solution. My plugin CustomJS is now available on the community store which allows you to write and reuse javascript across Obsidian, and it works on mobile! :tada: Check it out and share any feedback you have.

5 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.