quickadd icon indicating copy to clipboard operation
quickadd copied to clipboard

[BUG] User scripts errors on Mobile (QuickAdd 0.5.0)

Open Elaws opened this issue 2 years ago • 8 comments

Description

On two scripts, I've noticed errors when trying to launch them on mobile.

To reproduce

Script 1

When launching your Movie and Series script, I get the following error :

Error: Failed to load. Cleartext HTTP traffic to www.omdbapi.com not permitted

It seems that writing :

const API_URL = "https://www.omdbapi.com/";

Instead of :

const API_URL = "http://www.omdbapi.com/";

At line 5 of your script solves the problem ?

Script 2

When launching my Video game script, I get the following error :

TypeError: QuickAdd.app.vault.adapter.getBasePath is not a function

Apparently there is a problem with line 46 :

var basePath = QuickAdd.app.vault.adapter.getBasePath();

Maybe this does not exist on mobile ? How could I solve this issue so that it works both on mobile and still on PC ? I need to write some data to obsidian configuration folder and thus need vault path to navigate to it.

Script 3

My books script works perfectly fine on mobile (Android 12 - One UI 3.0) thanks to your 0.5.0 update !

Environment:

  • Device: Galaxy S20FE
  • OS: Android 12 - One UI 3.0

Elaws avatar Feb 05 '22 15:02 Elaws

Hi @Elaws,

Thank you for reporting this!

For script 1, I've updated to use HTTPS and confirmed that it works. Thank you!

For script 2, that function doesn't seem to be available on mobile, just as you wrote. I suppose the closest thing is app.vault.basePath, which works on mobile. It returned the name of my vault when I used on it on my phone. Besides this, I'm not sure what is available.

And I'm glad to hear script 3 works entirely! :)

chhoumann avatar Feb 05 '22 16:02 chhoumann

Thanks for your quick reply @chhoumann !

I've tested the following :

var basePath = QuickAdd.app.vault.basePath;

Unfortunately, it returns undefined (on both PC and mobile) : maybe I'm doing something wrong ?

Elaws avatar Feb 05 '22 21:02 Elaws

Sure thing, @Elaws :)

My mistake, I believe it's actually: QuickAdd.app.vault.adapter.basePath

chhoumann avatar Feb 05 '22 21:02 chhoumann

Thanks a lot @chhoumann, it seems to work ^^

Unfortunately, I get stuck a few lines later at :

const path = require('path');

[...]

var basePath = QuickAdd.app.vault.adapter.basePath;
var relativePath = QuickAdd.app.vault.configDir;

savePath = path.normalize(`${basePath}/${relativePath}/igdbToken.json`);

Where I get the following error :

TypeError: Cannot read properties of undefined (reading 'normalize')

For better cross-platform compatibility, I suppose it's better to use that path.normalize, but somehow this can't be used on mobile ?

Elaws avatar Feb 05 '22 21:02 Elaws

Awesome!

I think anything node.js doesn't work on mobile. I believe there's something in the Obsidian API for path normalization, but I'm not certain.

chhoumann avatar Feb 05 '22 21:02 chhoumann

Oh you are right, it seems there is :

/**
 * @public
 */
export function normalizePath(path: string): string;

However, I'm unsure how to invoke this in a QuickAdd script, I've tried :

async function start(params, settings) {
	QuickAdd = params;

	[...]

	savePath = QuickAdd.app.normalizePath(...);

But it leads to TypeError: QuickAdd.normalizePath is not a function. Maybe I have to somehow include Obsidian API to the QuickAdd script ?

Elaws avatar Feb 05 '22 21:02 Elaws

@Elaws Exactly that function is actually a bit harder, I believe. It's not accessible from the window object, unlike app.

I just released a version which exposed the Obsidian API in scripts. Then you should be able to access the function like so:

module.exports = async (params) => {
    // params.obsidian.normalizePath
}

chhoumann avatar Feb 08 '22 18:02 chhoumann

Wow @chhoumann, this is great : thank you very much !

Thanks to your help, I've finally been able to make script n°2 work on mobile, using the following (approximately) :

async function start(params, settings) {

[...]

var relativePath = params.app.vault.configDir;
savePath = params.obsidian.normalizePath(`${relativePath}/fileName.json`);

[...]

if(await params.app.vault.adapter.exists(savePath))
{ 
    userData = JSON.parse(await params.app.vault.adapter.read(savePath));
} 
else {
    [...]
    await params.app.vault.adapter.write(savePath, JSON.stringify(userData));
}

Not sure if this is clean, but it seems to work on Windows, Android 12 and iPad ^^

I thought I'd need full Obsidian API access to read/write file, but these functions are part of app.vault, which you already expose.

Elaws avatar Feb 09 '22 00:02 Elaws

I've worked around the getBasePath() problem with getFullPath(), works for me on android and pc.

const date = new Date();
const today = date.getFullYear() + '-' + date.getMonth()+1 + '-' + date.getDate();
const file = this.app.vault.getAbstractFileByPath("logs/" + today + "-1.md");
const filepath = this.app.vault.adapter.getFullPath("/") + file.path;

vacy avatar Jan 17 '23 00:01 vacy

@Elaws how did you open the console on your mobile?

vacy avatar Jan 27 '23 13:01 vacy

@chhoumann app.fileManager.processFrontMatter is also not available on mobile, works on desktop though

vacy avatar Feb 05 '23 14:02 vacy