gDBView is not defined and xul - javascript

I'm trying to write a Thunderbird extension using XUL, a custom button that accesses the currently shown email message body and does something with it.
Apparently this would be possible using the global variable gDBView, as in the Display Mail User Agent extension:
var msgURI = null ;
if ( gDBView )
{
msgURI = gDBView.URIForFirstSelectedMessage ;
}
if ( msgURI == null )
{
return ;
}
var messenger = Components.classes["#mozilla.org/messenger;1"].createInstance (Components.interfaces.nsIMessenger ) ;
var msgService = messenger.messageServiceFromURI ( msgURI ) ;
Unfortunately if in my extension I replicate the row:
msgURI = gDBView.URIForFirstSelectedMessage ;
I get the following error from the Thunderbird console:
gDBView is not defined
Is there a reason why this happens? And is this the best (and correct) way to access a mail body?

You're probably running your script in the wrong context, as Wladimir correctly guessed. An easy way to check that gDBView exists is, in the menus, to hit Tools > Error Console, then paste top.opener.gDBView, then hit Enter. This returns (for me) [xpconnect wrapped (nsISupports, nsIMsgDBView, nsITreeView)] which means the object indeed does exist.

Related

Javascript within AppleScript 'missing value' (for clicking button in Safari)

I have the following AppleScript with Javascript contained:
set buttontext to "Add Option"
set buttonloc to 1
tell application "Safari"
activate
tell window 1
do JavaScript "var buttonTags = document.getElementsByTagName(\"button\");
var searchText = \"" & buttontext & "\";
var found;
for (var i = 0; i < buttonTags.length; i++)
{if (buttonTags[i].textContent == searchText) {
found = buttonTags[i]; break;
}
}
buttonTags[" & buttonloc & "].click();"
end tell
end tell
It compiles fine but upon execution there is no action and I receive a 'missing value' error. It is designed to traverse a web page in Safari and allow a user to specify the button text and rank to be able to click it.
When execute this Javascript directly in Safari's dev console it works, but I need to have it in AppleScript to wrap it into a longer routine.
Advice appreciated!
I have managed to solve this with amending the end of the JS to:
buttonTags[" & buttonloc & "].click();" in current tab

Internet Explorer - Syntax Error - AngularJS

I am working on a web site using angularJS. The project is made on Visual Studio so the server is IIS.
When using Chrome, everything is working fine. But my boss wants also this site to work fine with Internet Explorer 11, in case of some clients are only using this browser.
The problem is that i have an error at an unexpected place when is use IE, for example :
Syntax Error : Caractère Incorrect ( that's french, it means Invalid character )
at getConnectedUser (http://localhost:54579/mypath/myfile.js:121:13)*
And if i open this file at line 121
vm.connectedUser = JSON.parse($cookies.get("userLogged"))
and the 13th character is the v from vm ( since i have some tabs )
the wole function is :
function getConnectedUser() {
vm.connectedUser = JSON.parse($cookies.get("userLogged"))
}
Which is quite normal according to me...
I don't know how to solve this, since the error does not seem to come from the code but from the interpretation of my browser.
I have read that some keywords were not supported to declare variables, like const. I use "let" to declare my variables and it is supported by IE 11 according to this site
EDIT : i also use "var" ( for example : var vm = this )
It would be very helpful if you have some ideas to find a solution.
Thanks for reading,
Sonny
So
I tried to apply what you guys asked, here is what i've got :
The problem actually comes from my cookies, IE can't read it correctly, I get undefined when trying to log my cookie's type, instead of getting "string".
So i went to a different part of the projet, where this cookie is created.
I try to log the cookie just after it is created so it can't be alterated:
var myObject = { "value" : "sonny" }
var stringified = JSON.stringify(myObject) // myObject is validated by jsonlint.com
try {
$cookies.put("userLogged", stringified, {})
console.log("ok")
}
catch (error) {
console.log(error)
}
// i got "ok" in logs , the try part is successful
console.log("fresh cookie : ")
console.log(typeof $cookies.get("userLogged"))
// On Chrome Console : string
// On Ie Console : undefined
console.log(typeof JSON.parse($cookies.get("userLogged")))
// On Chrome Console : object
// On Ie Console: Not logged , i have the Syntax Error Message
So the problem seems to be that IE can't read the cookie i just set before.
Using IE developers tools , i can see the cookie line :
Value : %7B%22value%22%3A%22sonny%22%7D
each special character ( '{', '"' and '}' ) is replaced by "%7B" , "%22" and "%7D"... It is exactly the same with chrome developers tools so i don't think it should be a problem.
Can IE actually read cookies using angular and ngCookies ?
As everyone mentioned in comments.
It is difficult to solve the issue with information provided.
So the best option could be using IE developer tools and debug your application.
Here is how I can troubleshoot the issue.
function getConnectedUser() {
// forces browser to stop at below line
debugger;
var loginInfo = $cookies.get("userLogged"); //put a debug point
// check for loginInfo existence and typeof loginInfo should be string
if (loginInfo && typeof loginInfo === 'string'){
vm.connectedUser = JSON.parse(loginInfo);
}
}
Put a break point in the getConnectedUser function
Check the value of $cookies.get("userLogged"), whether it is returning data or not
yes => check the type of data. It must be a valid JSON string format.
No => do the corresponding actions

Getting the current domain name in Chrome when the page fails to load

If you try to load with Chrome: http://sdqdsqdqsdsqdsqd.com/
You'll obtain:
ERR_NAME_NOT_RESOLVED
I would like, with a bookmarklet, to be able to get the current domain name and redirect it to a whois page in order to check if the domain is available.
I tried in the console:
window.location.href
but it outputs:
"data:text/html,chromewebdata"
Is there any way to retrieve the failed URL?
The solutions given by others didn't work (maybe because I was getting a different error or have a newer version: Chrome 55):
document.querySelector('strong[jscontent="hostName"]').textContent
but the same can be achieved via:
document.querySelector('#reload-button').url
A potentially more future-proof version (from Thomas's comment)
loadTimeData.data_.summary.failedUrl
So a cross-version solution incorporating all workarounds:
var url = (l‌​ocation.href === 'data‌​:text/html,chromeweb‌​data'
&& loadTimeData.data_.summary.failedUrl
|| document.querySelector('#reload-button').url
) || location.href;
var hostname = (l‌​ocation.href === 'data‌​:text/html,chromeweb‌​data'
&& loadTimeData.data_.summary.hostName
|| document.querySelector('strong[jscontent="hostName"]').textContent
) || location.hostname;
On the Chrome error page, location.href doesn't point to the domain you tried to visit, since it's an internally-hosted page.
However, the domain name you tried to visit is available if you expand the "Show Details" link.
You can run this code in console (or a bookmarklet) to parse out the domain name:
document.querySelector('strong[jscontent="hostName"]').textContent
A modified version of nderscore's since you'll need to have an if statement for the return of the correct one.
function getUrl () {
if(window.location.hostname == "") {
return document.querySelector('strong[jscontent="hostName"]').textContent
} else{
return window.location.href;
}
}
While looking in source code of html page using Developer Tools (right-click on a web page, and select Inspect Element), I've found that full original failed URL is in a variable called
loadTimeData.data_.summary.failedUrl
In my case I have to update some 'incorrect part' of auto generated URL to 'correct part'. So I've created bookmarlet like this:
javascript: location.assign(loadTimeData.data_.summary.failedUrl.replace('IncorrectPart','CorrectPart'));

How to get access log of PAC(proxy auto config)

I'm using Chrome, and I want monitor brower access log for myself ( I suspect some extension send url in background, etc ), I try to use Privoxy, but it can't logging HTTPS url
I think PAC is the only way for logging original url, I use PAC for few years, but I still don't know how to debug it or logging access. How I can do it ? (or any way to logging original url)
I guess some code:
function FindProxyForURL(url, host) {
/*
logging variable 'url' there, but how to write it to file or somewhere ?
// some logging code
*/
if (0
|| shExpMatch(url, "*facebook*")
|| shExpMatch(url, "*google.com/*")
|| shExpMatch(url, "*twitter/*")
) {
return "SOCKS5 127.0.0.1:55777; SOCKS5 127.0.0.1:55888";
}
return "DIRECT";
}

Testing and editing JavaScript (both standalone and in HTML) in SciTE?

Whenever I try to run a '.js' file in SciTE (Scintilla Text Editor) I almost always get an error stating that certain variables are undefined. I'm guessing that SciTE doesn't have many JavaScript libraries, but I'm not sure.
A few searches yielded me these two blog posts on how to get SciTE to print JavaScript test to its output, rather than just opening a web browser when you press F5 to test the code.
I tried them both, but I either got the same errors as before with the first post's solution, or I got an error that said "'jrunscript' is not recognized as an internal or external command, operable program or batch file" with the second method.
So, is it possible to test JavaScript code in SciTE and print the JavaScript output (or errors) to SciTE's output?
Simple example code I've tried:
console.log("test")
The error message I received for this: 'Microsoft JScript runtime error: 'console' is undefined'
What is SciTe?
SciTE is a SCIntilla based Text Editor. Lua is embedded with SciTe which allows you to access the Scintilla API.
# lua code example
`command.go.*.js=jrunscript $(FileNameExt)`
How to run js code in SciTe?
Create a file called testConsole.js with the following content.
var console = console || {};
console.log = ( console.log || function( str ){
if( typeof print == "function" ){
print( "LOG: " + str + "\n" );
}
return "LOG: " + str;
});
console.log( "Javascript works." );
Open testConsole.js in SciTe.
To run the code, press F5 or click Tools > Go.
An output window should appear showing
LOG: Javascript works.
How do I configure SciTe to run javascript?
I'm using SciTe 3.2.0. Located here
In wscite\wsite320\cpp.properties at line: 424
change:
command.go.*.js=cscript /nologo $(FileNameExt)
to:
command.go.*.js=jrunscript $(FileNameExt)
if you want to use node.js, then change it to
command.go.*.js=node $(FileNameExt)
Make sure that you have the jrunscript or node in your path for the environment variables.
Tutorial here
Do I have jrunscript?
Here's the easiest way to check.
Open up run > type in cmd > type jrunscript.
js> should appear on the screen.
jrunscript.exe should be located here.
C:\Program Files\Java\jdk1.7.0_01\bin\jrunscript.exe
Download the lastest Java SDK if you can't find it.
Error Messages
What does 'Microsoft JScript runtime error: 'console' is undefined'
This means Microsoft JScript ran your javascript and couldn't find the variable console.
Define console to get rid of the error message.
var console = console || {};
console.log = ( console.log || function( str ){
if( typeof print == "function" ){
print( "LOG: " + str + "\n" );
}
return "LOG: " + str;
});
Microsoft JScript might be located here:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\jsc.exe
Error: Input Error: There is no file extension in "location"
Solution: You need to configure the cpp.properties file for javascript.
Error: script file test is not found
Solution: Rename the file. Make sure that it doesn't have any spaces.

Categories

Resources