location.href="javascript:Function(); isn't working for Firefox extensions
Asked Answered
E

0

1

In Chrome web-browser extensions I can use the next method for calling/triggering a web-site's (page) js function:

location.href="javascript:SomeFunction(); void 0";

It works great.

But it doesn't work for Firefox

Though in old Firefox addons I could call web-site's js functions in similar way like this:

var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                  .getService(Components.interfaces.nsIWindowMediator);
var mainWindow = wm.getMostRecentWindow("navigator:browser");
mainWindow.gBrowser.loadURI("javascript:SomeFunction(); void 0");

But now it's all about Firefox Web Extensions and I wanted location.href="javascript:SomeFunction(); void 0"; to be working in FF

Elma answered 3/3, 2017 at 11:41 Comment(6)
Hmmm...Interesting. I had not considered using that method of calling web page functions. I'll have to play around with it to see why that works in Chrome, but not Firefox.Ambulance
@Ambulance #40572565 - it's just adding script into page, but I have to call function only if I use the context menu items or middle mouse clicks, so there is no way that adding script to a page will solve my problem, I need something like location.href="javascript:SomeFunction(); void 0"; so could call the function whenever I want, not just on load dom/pageElma
I'm not sure what your issue is with the proposed duplicate. The proposed duplicate shows how to execute a function on the page whenever you desire by dynamically adding a <script> element containing the code which you desire to run. You are effectively adding <script>SomeFunction();</script> to the DOM. While somewhat more complicated than what you stated, it can be used in the same way. It executes when you add it to the DOM, not upon DOM load/page load.Ambulance
@Ambulance oh sorry, forgot how dom works (I have not been playing with web for a while). But I think we can leave my question about why alternative method location.href="javascript:...; void 0"; isn't working in FFElma
If you want to reword the question (i.e. change the title, and some rewording) so that it is explicitly "Why does X not work", that would be good. If you do, ping me. I'll retract my close vote and see if I have some time to go exploring for an answer. There are lots of things on my to-do list right now, but someone else might have an answer prior to me getting to it. However, the current title and question wording strongly pushes me to read it as "How can I do Y".Ambulance
Related, but not a duplicate: Calling webpage JavaScript methods from browser extensionAmbulance

© 2022 - 2024 — McMap. All rights reserved.