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

Problem running JavaScript from iCloud #582

Closed
lparikka opened this issue Nov 17, 2023 · 8 comments
Closed

Problem running JavaScript from iCloud #582

lparikka opened this issue Nov 17, 2023 · 8 comments

Comments

@lparikka
Copy link

I have Mac 12.6.8 with Safari 17.0 and UserScripts 4.4.5.

My scripts are stored in iCloud. I have at the moment only one JavaScript which loads very uncertainly. I have to reload the page many times to have the script finally inject.

The UserScripts interface is also often stuck as you see in the image:

image

The script is something like this:

// ==UserScript==
// @name        Button
// @namespace   all
// @version     1
// @grant       none
// @match       https://mysite.com/*
// ==/UserScript==

(function(){
    'use strict'

  window.addEventListener('load', () => {
    addButton('DoSomething', dosomething,'www');
    })

    function addButton(text, onclick, sub, cssObj) {
        let pos = '';
        let top = '2px';
        if(text == 'DoSomething') {
            pos = '230px';
        } 
        cssObj = cssObj || {position: 'absolute', top: top, left:pos, 'z-index': 3, padding:'5px 5px'}
        let button = document.createElement('button'), btnStyle = button.style
        document.body.appendChild(button)
        button.innerHTML = text
        button.onclick = onclick
        Object.keys(cssObj).forEach(key => btnStyle[key] = cssObj[key])
        return button
    }
    
    function dosomething() {
    let m = document.URL.match(/^.*TextElement\/(.*)?\/.*$/)
        if (m[1]) {
            window.open('https://mysite.com/action?id=' + m[1], 'name', 'modal=yes');
        }
    }

}());



@ACTCD
Copy link
Collaborator

ACTCD commented Nov 17, 2023

This situation should be irrelevant to your specific user script.

One situation we currently know is that when the device space is insufficient, the automatic eviction mechanism of iCloud documents will cause the extension to not run as expected (when the save location is specified as the iCloud directory).

The above situation has been tracked in #424. But based on the limited information you've provided, I can't be sure you're experiencing the same situation.

If possible, please add more details and provide popup and native log information for reference. Please refer to the log acquisition method in #409.

@lparikka
Copy link
Author

I don't find any errors in popup or native logs. Maybe I need to remove the extension and reinstall it and try with local scrips folder.

@ACTCD
Copy link
Collaborator

ACTCD commented Nov 18, 2023

Until more valid information is available, I cannot be of further assistance to you.

It would be a good idea to try using a local folder.

If you find anything new please keep commenting.

Let me close this issue for now.

@ACTCD ACTCD closed this as completed Nov 18, 2023
@lparikka
Copy link
Author

I reinstalled UserScripts and now the popup opens without problems. The script is injected only after I open the browsers developer tools, though. I tried with an CSS script on the same page and it works without developer tools open.

How could I check why doesn't the script inject? The script is like in my opening questions.

@ACTCD
Copy link
Collaborator

ACTCD commented Nov 21, 2023

Interesting, I don't know why it would have anything to do with whether or not devtools is opened.

Try just using the simple script below and see if it works?

// ==UserScript==
// @name               DEMO.Alert-URL
// @description        Demo user script alert URL.
// @author             Userscripts
// @version            1.0.0
// @match              *://*/*
// @grant              none
// @inject-into        content
// @run-at             document-start
// ==/UserScript==

(function () {
	"use strict";
	alert("DEBUG.Alert-URL:\n\n" + location);
})();

@ACTCD
Copy link
Collaborator

ACTCD commented Nov 21, 2023

OK, review your script again.
You didn't specify @run-at, so it uses the default document-end, so when the script is injected, the page may already be loaded, and the load event was emitted before you added the listener.
So your code may not be called at all.
The advice is, whenever the script inject, always check the document.readyState.

@lparikka
Copy link
Author

It get's interesting. I added the alert-box in the beginning of the failing script and it is shown even when the tools are closed, but the buttons are not drawn until I open the developer tools and reload the page. The @run-at setting makes no difference.

The page is a React application as far as I know, if that has something to do with the issue.

@ACTCD
Copy link
Collaborator

ACTCD commented Nov 21, 2023

Like I said above, did you check document.readyState ?

The code logic will like:

if (document.readyState === "complete") {
    yourFunc();
} else {
    window.addEventListener('load', yourFunc);
}

Also, any elements you add may be removed by the page script.
Add appropriate logging to your code to see what is being executed.

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

No branches or pull requests

2 participants