Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Elaws opened this issue Feb 5, 2022 · 8 comments
Open

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

Elaws opened this issue Feb 5, 2022 · 8 comments
Assignees
Labels

Comments

@Elaws
Copy link

@Elaws Elaws commented Feb 5, 2022

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
@chhoumann
Copy link
Owner

@chhoumann chhoumann commented Feb 5, 2022

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! :)

@Elaws
Copy link
Author

@Elaws Elaws commented Feb 5, 2022

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 ?

@chhoumann
Copy link
Owner

@chhoumann chhoumann commented Feb 5, 2022

Sure thing, @Elaws :)

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

@Elaws
Copy link
Author

@Elaws Elaws commented Feb 5, 2022

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 ?

@chhoumann
Copy link
Owner

@chhoumann chhoumann commented Feb 5, 2022

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.

@Elaws
Copy link
Author

@Elaws Elaws commented Feb 5, 2022

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 ?

@chhoumann
Copy link
Owner

@chhoumann chhoumann commented Feb 8, 2022

@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
}

@Elaws
Copy link
Author

@Elaws Elaws commented Feb 9, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants