nntoan / scriptify

Automatically exported from code.google.com/p/scriptify
MIT License
0 stars 0 forks source link

Works strange with @run-at document-start #5

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Test UserScript:

// ==UserScript==
// @name        Test Scriptify
// @namespace   dev/null
// @include     https://github.com/*
// @version     0.1
// @run-at      document-start
// @grant       none
// ==/UserScript==

if("console" in window && "log" in console)
    console.log("Test Scriptify");

Test code:

_log("setTimeout()");
setTimeout(function() {
    _log("setTimeout() done");
}, 0);
_log("addTab()");
gBrowser.selectedTab = gBrowser.addTab("about:");
_log("addTab() done");

function _log(s) {
    var d = new Date();
    var ms = d.getMilliseconds();
    var timestamp = d.toLocaleFormat("%M:%S:") + "000".substr(String(ms).length) + ms;
    Services.console.logStringMessage("[test] " + timestamp + " " + s);
}

Output:

[test] 07:42:127 setTimeout()
[test] 07:42:127 addTab()
[test] 07:42:134 setTimeout() done
[test] 07:42:159 addTab() done

And without Scriptify "setTimeout() done" is always after "addTab() done" 
because addTab() should be synchronous.

Also see https://github.com/Infocatcher/Private_Tab/issues/110

Original issue reported on code.google.com by infocatc...@gmail.com on 19 Oct 2013 at 7:04

GoogleCodeExporter commented 9 years ago
Sorry, this report doesn't make much sense to me. Can you provide minimal 
example extensions as a test case? Also, Scriptify doesn't directly use 
Greasemonkey metadata, and the "run-when" directive in scriptify.json does not 
support "document-start", so I don't see how that can be relevant.

Original comment by maglion...@gmail.com on 19 Oct 2013 at 7:29

GoogleCodeExporter commented 9 years ago
> and the "run-when" directive in scriptify.json does not support 
"document-start"
Hmmm... may be this may happens for any UserScript.

Example extension: http://infocatcher.ucoz.net/test/ext/test-scriptify.xpi
(from "Test UserScript" described above)

Then set devtools.chrome.enabled = true in about:config, open Tools - 
Web-Developer – Scratchpad (Shift+F4), set Environment - Browser and execute 
"Test code" from my first message.

setTimeout(..., 0) will happens before addTab().

(I need some way to detect opened tabs after drag-and-drop or calls like 
BrowserOpenTab())

Original comment by infocatc...@gmail.com on 19 Oct 2013 at 7:42

GoogleCodeExporter commented 9 years ago
Well, in the case of that extension, the "run-when" directive is simply 
ignored, since it doesn't contain a recognized value. As for the console 
output, can you try using `dump()` instead? The console service does some 
queueing, and does not dispatch the messages synchronously. Any number of 
things could be going on.

My best guess is that, if the Scriptify shim is causing this, something is 
causing `processNextEvent()` to be called while scripts are being loaded. The 
only code in Scriptify which should cause that is the synchronous 
`XMLHttpRequest` which occurs if `getResourceText()` is called (which does not 
happen in the example add-on). The only other possible explanation I can think 
of would be the subscript loader similarly spinning the event loop which, to my 
knowledge, it never has and still does not.

Original comment by maglion...@gmail.com on 19 Oct 2013 at 8:52

GoogleCodeExporter commented 9 years ago
> can you try using `dump()` instead?

With
function _log(s) {
    ...
    dump("[test] " + timestamp + " " + s + "\n");
}

With enabled test extension:
[test] 02:47:606 setTimeout()
[test] 02:47:607 addTab()
[test] 02:47:613 setTimeout() done
[test] 02:47:634 addTab() done

[test] 02:51:629 setTimeout()
[test] 02:51:629 addTab()
[test] 02:51:636 setTimeout() done
[test] 02:51:652 addTab() done

With disabled test extension:
[test] 02:56:142 setTimeout()
[test] 02:56:142 addTab()
[test] 02:56:164 addTab() done
[test] 02:56:167 setTimeout() done

[test] 02:58:580 setTimeout()
[test] 02:58:580 addTab()
[test] 02:58:591 addTab() done
[test] 02:58:603 setTimeout() done

Anyway, this is just a simple testcase, because I already see result of this 
issue with installed Private Tab extension:
https://addons.mozilla.org/addon/private-tab/
+ already posted https://github.com/Infocatcher/Private_Tab/issues/110

Original comment by infocatc...@gmail.com on 19 Oct 2013 at 9:10

GoogleCodeExporter commented 9 years ago
> With ...
> dump(...)
And browser.dom.window.dump.enabled = true, of course.

Original comment by infocatc...@gmail.com on 19 Oct 2013 at 9:12

GoogleCodeExporter commented 9 years ago
> Well, in the case of that extension, the "run-when" directive is simply 
ignored, since it doesn't contain a recognized value.

You're right, the same happens for extensions, based on user.js without @run-at 
directive:
https://github.com/Infocatcher/Private_Tab/issues/110#issuecomment-26698774

Original comment by infocatc...@gmail.com on 21 Oct 2013 at 8:08

GoogleCodeExporter commented 9 years ago
Seems like I can't modify issue's title...
Please change it to something like "Works strange with setTimeout() and 
gBrowser.addTab(): setTimeout's callback is called before adding of a new tab".

Original comment by infocatc...@gmail.com on 21 Oct 2013 at 8:13

GoogleCodeExporter commented 9 years ago
Interesting, works fine with "manager = new GlobalManager;":

--- a/bootstrap.js
+++ b/bootstrap.js
@@ -944,11 +944,11 @@

             // The Message manager on Gecko <8.0 won't accept message listeners
             // from sandbox compartments.
-            if (Services.vc.compare(Services.appinfo.platformVersion, "10.*") 
< 0
-                    && Services.appinfo.name != "Fennec")
+            //if (Services.vc.compare(Services.appinfo.platformVersion, 
"10.*") < 0
+            //        && Services.appinfo.name != "Fennec")
                 manager = new GlobalManager;
-            else
-                manager = new SlaveDriver;
+            //else
+            //    manager = new SlaveDriver;
         }

         this.shutdown = function shutdown(data, reason) {

Original comment by infocatc...@gmail.com on 21 Oct 2013 at 8:56