Templater icon indicating copy to clipboard operation
Templater copied to clipboard

User Script Functions are not working on mobile

Open selfire1 opened this issue 2 years ago • 10 comments

Plugin informations:

  • OS: Android
  • Templater version: 1.11.3
  • Obsidian version: 1.1.1 (44)
  • Templater settings:
    • Template: "10 Meta/Templates/testing.md"
    • User function: "10 Meta/Templates/Scripts/notice.js"

Content of the template (testing.md):

<%* tp.user.notice(tp) %>

Content of the script (notice.js):

async function notice(tp) {
    const text = await tp.system.prompt("What's Good?")
    new Notice(text, 5000)
}

module.exports = notice

Describe the bug The function doesn't run with the following error:

[error] Templater Error: Template parsing error, aborting. 
 tp.user.notice is not a function

Expected behavior The user function runs, displaying a prompt.

Additional context

  • I have tested this with other scripts as well, none work.
  • The template folder is set to 10 Meta/Templates
  • The script folder is set to 10 Meta/Templates/Scripts
  • The script is recognised by Tenplater in the settings: Screenshot_20220316-173350~2.jpg

Thanks for the great work with Templater. I'd love to use user functions on mobile too, but always run into an error.

selfire1 avatar Mar 16 '22 11:03 selfire1

I do have the same problem. If this is a limitation that javascript can not be run from the vault, it should probably be documented in the Documentation.

Tiim avatar Apr 01 '22 08:04 Tiim

I am having this same problem.

pjkaufman avatar Apr 17 '22 15:04 pjkaufman

It seems it is disabled on mobile: https://github.com/SilentVoid13/Templater/blob/master/src/core/functions/user_functions/UserFunctions.ts#L29-L34

pjkaufman avatar Apr 17 '22 16:04 pjkaufman

It looks like all I needed to do was remove the part about only getting user functions for desktop and it worked for me. I am going to make a PR to get that removed. It may be that iOS had problems or that there is a UT that fails. Here is to hoping there will be no issue with accepting the change.

pjkaufman avatar Apr 17 '22 17:04 pjkaufman

It looks like all I needed to do was remove the part about only getting user functions for desktop and it worked for me. I am going to make a PR to get that removed. It may be that iOS had problems or that there is a UT that fails. Here is to hoping there will be no issue with accepting the change.

I have this issue, too. I have no clue how I can resolve it using your fix because I’m not very literate, but I guess I will wait until Void sees this so I can use user scripts on Mobile. THANKS!

Lennon-ruthven avatar Apr 19 '22 11:04 Lennon-ruthven

Testing the fix on iOS and I still can't run user scripts.

shabegom avatar Sep 02 '22 13:09 shabegom

So https://silentvoid13.github.io/Templater/user-functions/overview.html is still correct, user defined scripts don't run on mobile?

tsteckenborn avatar Jun 29 '23 07:06 tsteckenborn

It works just fine on Android.

Edit: I don't have an iOS device, but it works on Android as I use it frequently on Android.

pjkaufman avatar Jun 29 '23 09:06 pjkaufman

I just made a user script on iOS, and to my surprise this seemed to work.

I get the following warning:

Templater Error: Template parsing error, aborting. Check console for more information

But the templater command and the user function are all evaluated correctly.

If anyone can tell me how to check the console on iOS I can provide more detailed info.

Plugin informations:

OS: iOS Templater version: 1.18.3 Obsidian version: 1.4.16(114) Templater settings: Template: "/templates/random-reflection-question.md" User function: "Scripts/select_random_line_from_file.js"

Content of the template (random-reflection-question.md):

## 
<%_*
fc = await tp.file.include("[[Reflection questions]]");
tR+= " "
tR+= await tp.user.select_random_line_from_file(fc);
-%>

Content of the script (select_random_line_from_file.js):

function getRandomStringFromFileContent(fileContent) {
  if (typeof fileContent !== 'string') {
    return "Invalid input: Please provide a valid string.";
  }

  stringList = fileContent.split('\n');
  if (!Array.isArray(stringList) || stringList.length === 0) {
    return "Invalid input: Please provide a non-empty array of strings.";
  }

  const randomIndex = Math.floor(Math.random() * stringList.length);
  return stringList[randomIndex];
}

module.exports = getRandomStringFromFileContent;

AdriaanRol avatar Jan 02 '24 16:01 AdriaanRol

To inspect the iOS app as well as read the console, you need iOS 16.4+ and a Mac. Instructions: https://webkit.org/web-inspector/enabling-web-inspector/

Zachatoo avatar Jan 02 '24 16:01 Zachatoo