insertElementAtSelection stopped working - javascript

In my Thunderbird Addon called PasteHyperlink, I have a routine that inserts an html element into the Message Compose Window.
This used to work in Thunderbird, but now I get this js error:
Error: TypeError: thiseditor.insertElementAtSelection is not a function
However, it seems that thiseditor gets defined because it doesn't launch the alert.
Here is the function's code which I've reduced to the basic functionality:
var thiseditor = gMsgCompose.editor;
if (!thiseditor){ alert("Dude, the gMsgCompose.editor is broken") };
let link = thiseditor.document.createElement("a");
link.setAttribute("href", "http://stackoverflow.com");
link.textContent = "Display Text";
thiseditor.insertElementAtSelection(link, false);
MDN has this documentation, but I can't find anything anywhere that talks about why this is broken or what changed under the hood in Thunderbird 45.
Why did this quit working, and what should I do instead?

Well, I think I figured it out. I changed this:
var thiseditor = gMsgCompose.editor;
to this:
var thiseditor = gMsgCompose.editor.QueryInterface(Components.interfaces.nsIHTMLEditor);
Funny thing, it worked the first way for quite a long time. I don't know what changed in Thunderbird, though.

Related

Add link to sharepoint suite bar - fail on first load

I'm trying to use JavaScript to add a custom link to the suite bar in SharePoint (SharePoint 2016 on premise). The code works fine except for the first time I load the SharePoint site. Here it is:
<script>
var raiseFunc = function() {
var link = document.createElement('a');
var linktext = document.createTextNode("Google");
link.href = "http://www.google.ca"
link.setAttribute("class", "o365button o365cs-nav-appTitle o365cs-topnavText");
var span = document.createElement('span');
span.appendChild(linktext);
span.setAttribute("class", "o365cs-nav-brandingText");
link.appendChild(span);
var temp = document.getElementById("Sites_BrandBar");
temp.parentElement.appendChild(link);
};
_spBodyOnLoadFunctions.push(raiseFunc);
</script>
When I refresh the page or navigate around, everything works. It just doesn't work when I open the page from a new window or tab. (I have the MDS feature disabled.) It seems like the suite bar is not available when the code runs for the first time, even though I'm delaying my code using the _spbodyOnLoadFunctions technique.
Any ideas?
Update: On Matt's suggestion in the comments, I put in try/catch to get any errors. No errors are reported but I did notice a change in behaviour. I did about 20 tests in a row and sometimes the new link is added fine, sometimes it is not, and sometimes it is added for a second and then disappears.
I also tried putting an alert in the code before this line:
var temp = document.getElementById("Sites_BrandBar");
The delay is execution makes it work so clearly it is a timing thing. I assume that sometimes the suite bar is not ready when the code runs. I added a timeout as a test:
setTimeout(function(){
var temp = document.getElementById("Sites_BrandBar");
temp.parentElement.appendChild(link);
}, 500);
This fixes the issue but I don't like the idea of using a timeout every time the page runs.
Any ideas on how to delay the code execution until the suite bar is ready?
As per Matt's comment, this worked:
"Try to make your add link into a function and use SP.SOD.executeFunc('sp.js','SP.ClientContext', yourFunctionToInsertHere); I believe it's the sp.js that is not loading before you are trying to add it. This should delay your add until it's loaded."
Thanks!
I think it's being done using a Custom Action, but can't be certain.
One way to test it is to put the file into a .js file and then add it as a Custom Action using the excellent script, which John Liu built.
http://johnliu.net/blog/2015/12/the-safest-future-proof-way-to-brand-your-sharepoint-and-sharepoint-online
The other way is to maybe use the PnP PowerShell cmdlets using;
Add-PnPJavaScriptBlock -Name SuiteBar -Script '<your code here>' -Sequence 9999 -Scope Site

Chrome Application is not closing

I have a close button for my chrome application and I've added the event listener to it. But now I am getting this error:
Uncaught TypeError: this.contentWindow.close is not a function
Coming from line 227 in the Chrome library itself
AppWindow.prototype.moveTo = $Function.bind(window.moveTo, window);
AppWindow.prototype.resizeTo = $Function.bind(window.resizeTo, window);
AppWindow.prototype.contentWindow = window;
AppWindow.prototype.onClosed = new Event();
AppWindow.prototype.onWindowFirstShownForTests = new Event();
AppWindow.prototype.close = function() {
this.contentWindow.close();
};
So I have multiple ways I've tried this first is as so
_.get = chrome.app.window.get.call(chrome,"main");
//second way
_.get = chrome.app.window.get("main");
//third way in dev panel
chrome.app.window.get("main").close();
//fourth way in dev panel
chrome.app.window.current().close();
Either way none of these will work because I think that there is something conflicting with the library itself. THOUGH everything works except for the close function.
Any suggestions as to why this would be?
Ok so looking further into my code I realized I made a very big hiccup like an amatuer and clogged the global scope. I initialized a variable close which in turn made window.close equal my new variable :0

NW.js Notification onclick dont execute on windows

Im building a app/site using NW.js (known as node-webkit) The docs say that under linux Notifikations onclick event handler dont get fired, and my tests shows that aswell. but when running on windows 7 (x86 bit) the event is not fire'd ether, while it all is running on osx and under chrome + firefox
it's easy to reproduce just run a sample with somthing along the lines of:
var noti = new Notifikation("foo titile");
noti.onclick = function() { console.log("bar") };
I ran into same problem and have been searching for an answer since yesterday night and even though I couldnt find the real answer for the "Why 'onClick()' event handler doesn't work" question, I found a workaround.
First of all, javascript is case sensitive. The appropriate usage of the 'click' event handler is 'onClick'
So the correct format is
//NOTE: I'm not sure if 'Notifikation' works here. Correct use of it is 'Notification'
var noti = new Notifikation("foo titile");
noti.onClick = function() { console.log("bar") };
Now, even though your spelling is correct, onClick event still didn't work for me on nwjs-v0.12.2-win-x64 , Windows 8.1.
What I tried as an alternative is the addEventListener(event, function) method.
In this case your code would be:
var noti = new Notifikation("foo titile");
function clicked() { console.log("bar") };
noti.addEventListener('click',clicked);
I know this question is 2 months old but I just wanted to answer in case someone else would face the same problem.

Javascript variable undefined in IE until console displayed

I have a small piece of code for a template project I'm working on. There are three separate buttons, that point to three separate locations. In order to make it easier for content providers, I have these buttons calling minimal routines to load the next page.
The code is as follows:
/* navigation functions here for clarity and ease of editing if needed */
prevURL = 'ch0-2.html';
nextURL = 'ch2-1.html';
manURL = 'ch1-2.html';
function prevPage() {
window.location = prevURL;
}
function nextPage() {
window.location = nextURL;
}
function goManager() {
window.location = manURL;
}
This works perfectly in Firefox and Chrome, but seems to fail in Internet Explorer.
I open up the developer tools in IE (F12) and am presented with the message:
SCRIPT5009: 'manURL' is undefined
The location information (line 43, character 13) points to the "window.location = manURL" part of the code.
However, once the developer tools are open, if I hit F5 to reload the page, the button works without error until I close IE and reopen it, where it once again fails to respond and gives the same "undefined" error.
I'm baffled. Anyone have any ideas?
UPDATE
I know the variable declaration is poor, and that I can use window.location.href instead. What is relevant here is that the other two pieces of code, which are identical in all of these significant ways, work perfectly either way.
epascarello has put me on the right track. by removing all console.log commands, everything starts working. I'm just wondering why this happens, and would like to be able to give epascarello credit for helping me.
IE does not have console commands when the developer window is not open. So if you have them in there the code will not run. It will error out.
You can either comment out the lines or add in some code that adds what is missing.
if (typeof console === "undefined") {
console = {
log : function(){},
info : function(){},
error : function(){}
//add any others you are using
}
}
Try setting
window.location.href
instead of just window.location.
Source:
http://www.webdeveloper.com/forum/showthread.php?105181-Difference-between-window.location-and-window.location.href
Always define variables before using them ,being explicit (expressing the intention) is a good practice,
so define your variables like this,
var prevURL = 'http://google.com';
var nextURL = 'http://msn.com';
var manURL = 'http://stackoverflow.com';
You can try using
window.location.href
refer to this post for difference
Suggestion:
Make a jsfiddle.net for us so we could guide you easily

window.open not working in IE

Apparently, this call to window.open is not valid under Internet Explorer. The Javascript code on my site is not running, I would assume it is due to that error.
The line it tells me the error is on, is the call to window.open, apparently an argument is not valid there.
$('.objeto').click(
function() {
var center = 'height=380,width=900,top='+((screen.width - 900)/2)+',left='+((screen.height - 380)/2);
var address = $(this).attr('id');
window.open (address,'Ver articulo', config=center);
}
);
The site runs fine under both Google Chrome, and Firefox.
In IE, you can't have spaces in your second variable (the new window's name).
Try:
window.open (address,'Ver_articulo', config=center);
Also worth re-iterating that IE9 (and possibly below) doesn't like hyphens ('-') in the window name (2nd parameter).
I know one of the comments mentioned this, but it's a bit buried - and it's one tip that just solved an issue for me.
I'm not sure what config is, you just need:
window.open (address,'VerArticulo', center);
Keep in mind though, it looks like your id attribute is invalid to get the effect here, you probably want to use something different, e.g. data-href="urlHere" on the element, if it's not an anchor already.
even thou it's kind a late with answer for OP, but for someone else stumbling across this post it might help:
Had exactly same problem as OP after trying to use "window.open" method. It turns out that Chrome is ok with original href tag with URL in it where IE seems to get confused with that. After removing href from link worked spot on.
CODE SAMPLE:
$(document).ready(function ()
{
$('a[rel^="external"]').each(function ()
{
var externalLink = $(this);
var externalLinkValue = externalLink.attr("href");
externalLink.unbind('click');
externalLink.removeAttr("href");
externalLink.click(function (event)
{
event.preventDefault();
followExtrenalLink = window.open(externalLinkValue,'_blank');
});
externalLink.hover(function ()
{
externalLink.css('cursor', 'pointer');
});
});

Categories