Why copy & paste dart2js' output to console doesn't work? - dart

compile the following code with dart2js -o test.js test.dart
open test.js, copy its content
open browser, go to stackoverflow.com
open dev tools, go to console tab
paste test.js's content into console, hit Enter
I expect it to click the "Ask Question" button, but it doesn't, why?
(The reason I want to do this is, I need some js, but I don't want to touch js.)
// test.dart
import 'dart:html';
void main() {
document.querySelector('#nav-askquestion').click();
}

I didn't dig very deep but I had the impression the generated code registers itself for a script loaded event and then executes "main" as event handler. I don't know JavaScript and browser behavior good enough to understand how this can work.
I got it working by running this code in the dev console
(function runTest() {
var s = document.createElement("script");
s.type = "text/javascript";
s.src = "test.js";
document.body.appendChild(s);}
)();
where the test.js (generated output from dart2js) file is in the same directory as the index.html.
The code adds the script tag referencing the dart2js output dynamically and the code in test.js is executed.

Related

How to access custom javascript functions via browser-console in Rails 6

For the sake of debugging the javascript-part of a Rails 6 (version 6.0.0.rc1) web application I want to use my custom javascript functions also in the Chrome console (aka. Inspect).
Back when I used just static HTML files to build a website (as opposed to using a web-framework like Rails as of right now) you would simply embed the JS file in the DOM like this
<!-- custom JS -->
<script src="js/custom.js"></script>
and could instantly access and execute all custom functions that were placed in this file.
Background:
The JS file is placed at the correct rails 6 specific directory as provided in this article: How to require custom JS files in Rails 6
Note:
The rails 6 application also uses the JS file already, since the browser shows the console log message.
Here is the full content of the JS file:
// app/javascript/packs/custom.js
console.log("loaded custom.js successfully")
function sayHello () {
console.log("Hello good Sir or Madam!")
}
Expectation: I am expecting to open the browser's (Chrome) console and be able to use the sayHello() function in the console.
However, when I do so, I get an error message in the console stating:
Uncaught ReferenceError: sayHello is not defined
Try something like
sayHello = ()=>{
console.log("Hello good Sir or Madam!");
}
then you can evoke in console:
>sayHello();

Thunderbird 67 Extension doesn't inject Content Script in the main tab

I'm making a Thunderbird Extension through the WebExtension Api (with popup) and i've some difficulties running a Content script to get some elements (attachments list of the current mail for example) directly from Thunderbird interface.
I've made a popup.html , a popup.js and a contentScript.js that runs when the popup is opened. Apparently contentScript.js doesn't run in the main Thunderbird tab, but runs smoothly when i open a new tab (with url) through code.
$(document).ready(function(){
console.log('Try to execute contentScript');
// -- create new tab --
// browser.tabs.create({
// url: 'https://example.org'
// });
// -- execute script in current tab --
browser.tabs.executeScript({
file: 'scripts/contentScript.js'
});
});
// from contentScript.js
console.log('contentScript.js - Injected');
In console i expected "contentScript.js - Injected" but this only appens when i am not in the main Thunderbird tab.
When i am in the main tab it shows only "Try to execute contentScript" then nothing appens, no errors.
Thunderbird extension are evolving right now from the old legacy method (overlay of xul files) to the actual WebExtension Api used by most of the browsers, but there are some differences: in Firefox the extension works without any problem, so i supposed that the main tab of Thunderbird is somehow protected from content injection.
My objective here is to get the attachments list and other elements directly from the interface, but apparently i can't.

How to deploy dart polymer with no index.html entry point

I have a dart web application using polymer. I can successfully run it with Dartium using boot.js. However, my index.html file is actually a Django template in another git repo for the project. Its uses template inheritance, among other things, so its not just a normal HTML file.
My goal is to have a Makefile compile the project on request. Currently, pub deploy will compile all the code, and it will run in non-dart browsers. However, my custom polymer elements do not end up being registered. They all show up as blank. Is this kind of setup even possible, that is, to not have an index.html entry point and build custom polymer elements? I could create a dummy buid.html to satisfy the entry-point requirement, but this seems like a sub-optimal solution.
My current buid.dart looks like:
import 'dart:io';
import 'package:polymer/component_build.dart';
import 'package:polymer/deploy.dart' as deploy;
main() {
build(new Options().arguments, [])
.then((_) => deploy.main());
}
and the output:
'package:polymer/component_build.dart': Error: line 68 pos 29: \
ambiguous reference: 'JSON' is defined in library 'dart:convert' \
and also in 'dart:io'
var message = JSON.encode([jsonMessage]);
The only way is to provide some HTML file as entry point. It doesn't matter when you use another HTML file in production if it contains the necessary script tags.

"document" in mozilla extension js modules?

I am building Firefox extension, that creates single XMPP chat connection, that can be accessed from all tabs and windows, so I figured, that only way to to this, is to create connection in javascript module and include it on every browser window. Correct me if I am wrong...
EDIT: I am building traditional extension with xul overlays, not using sdk, and talking about those modules: https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules
So I copied Strophe.js into js module. Strophe.js uses code like this:
/*_Private_ function that creates a dummy XML DOM document to serve as
* an element and text node generator.
*/
[---]
if (document.implementation.createDocument === undefined) {
doc = this._getIEXmlDom();
doc.appendChild(doc.createElement('strophe'));
} else {
doc = document.implementation
.createDocument('jabber:client', 'strophe', null);
}
and later uses doc.createElement() to create xml(or html?) nodes.
All worked fine, but in module I got error "Error: ReferenceError: document is not defined".
How to get around this?
(Larger piece of exact code: http://pastebin.com/R64gYiKC )
Use the hiddenDOMwindow
Cu.import("resource://gre/modules/Services.jsm");
var doc = Services.appShell.hiddenDOMWindow.document;
It sounds like you might not be correctly attaching your content script to the worker page. Make sure that you're using something like tabs.attach() to attach one or more content scripts to the worker page (see documentation here).
Otherwise you may need to wait for the DOM to load, waiting for the entire page to load
window.onload = function ()
{
Javascript code goes here
}
Should take at least diagnose that issue (even if the above isn't the best method to use in production). But if I had to wager, I'd say that you're not attaching the content script.

How to make code in a Firefox Extension execute on a timer

Today is my first day working with firefox extensions.
Basically I am making an extension that will be used on an internal network to check a web server for new notifications.
I used the wizard on the mozilla page to make a skeleton extension and then mainly edited overlay.js with some ajax code.
I am using the "load" event listener to call a setTimeout to my ajax call which then loops with setTimeouts.
The problem appears to be that the "load" event listener is executed on each new browser window. I just want one global timer for this to work off of.
Any ideas?
Update:
I found this: https://developer.mozilla.org/en/JavaScript_code_modules/Using
which seems like what i would want. The problem is I can't figure out how to import the jsm file. What is the directory structure?
Update:
When trying this:
chrome.manifest
content spt chrome/content/
skin spt classic/1.0 chrome/skin/
locale spt en-US chrome/locale/en-US/
overlay chrome://browser/content/browser.xul chrome://spt/content/ff-overlay.xul
style chrome://global/content/customizeToolbar.xul chrome://spt/skin/overlay.css
resource mycontent chrome/content/
First 5 lines of chrome/content/overlay.js
try{
Components.utils.import("resource://spt/mycontent/ajax.jsm");
}catch(err){
alert(err);
}
I get this error:
[Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXPCComponents_Utils.import]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: chrome://spt/content/overlay.js :: :: line 2" data: no]
Or when if I remove the resource alias from chrome.manifest and use this at the beginning of overlay.js
try{
Components.utils.import("chrome://spt/content/ajax.jsm");
}catch(err){
alert(err);
}
I get this error:
[Exception... "Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIXPCComponents_Utils.import]" nsresult: "0x80070057 (NS_ERROR_ILLEGAL_VALUE)" location: "JS frame :: chrome://spt/content/overlay.js :: :: line 3" data: no]
Yes, if you have code that should be shared between windows (and should not be executed when a new window is loaded) and that don't need access to the chrome, use JavaScript code modules.
You can import your modules with:
Components.utils.import("resource://youraddon/your_module.jsm");
provided that you setup resource in your chrome.manifest. E.g. if you add
resource youraddon modules/
then you the file must be stored in /path/to/your/addon/modules/your_module.jsm.
Further notes:
Code modules don't have to have the file extensions .jsm. You can leave it .js. Works better sometimes with certain editors (and syntax highlighting etc).
Afaik you cannot use setTimeout in a module as it has no access to the window object. I suggest to use nsITimer.

Resources