Getting the current page's window with the low level api of the Firefox SDK - firefox-addon

I'm creating a Firefox extension for adding some functionality to certain Web pages. I need to check that some elements do exist and highlight them, so I'm using xpath to check and locate them. I know about manipulating tabs and the content through tabs and ports, but I really need to use the low level API and do it without ports. The thing is, I don't know how to get the current opened tab window (I can also open the tab, but I'm not getting the window). I already tryed to open a tab and :
tabs.open({
url: url,
onOpen: function onOpen(tab) {
// get the XUL tab that corresponds to this high-level tab
var lowLevelTab = viewFor(tab);
var browser = tab_utils.getBrowserForTab(lowLevelTab);
var doc = browser.contentDocument;
console.log(doc); //THIS IS AN EMPTY DOC
// get the most recent window. This give me a XUL window, and I can't sucessfully execute eval on that...
var win = utils.getMostRecentBrowserWindow();
}})
I sawa lot of methods for retrieving different kinds of windows, but I'm not finding the explanation about the differences. E.g. Chroe window, XUL window, NSI window, base window...I just need the current Web page's document window.
Any clarification is welcome.
Thanks in advance,

I just needed to listen for another tab event:
onReady: function onOpen(tab) {
var content = utils.getMostRecentBrowserWindow().content;
var domInstances = content.document.evaluate(me.getTemplateXpath(), content.document, null, 4, null);
var res = domInstances.iterateNext();
while (res) {
console.log(res);
res.style["background-color"] = "orange";
res = domInstances.iterateNext();
}
callback();
}

Related

Master menu is not visible on mobile devices but is visible on InternetExplorer

I'm working on a SAPUI5 App showing some data in a MasterDetailView using the sap.m.splitapp. My Problem is quit equal to the question Master menu is not visible on mobile devices but is visible on tablet.
In my case, I have the situation, that I'm going to create a MasterPage with a MasterController depending on requested data. Depending on these data I create dynamically the DetailPages in a function of the MasterController.
for(var i=0;i<adata.inhaltList.length;i++){var DetailPage = new sap.m.Page({
title: masterString,
showNavButton: true,
navButtonPress:function () {
var oSplitApp = this.getView().getParent().getParent();
var oMaster = oSplitApp.getMasterPages()[0];
oSplitApp.toMaster(oMaster, "flip");
}
}); };
On my test device, everything is working fine except the fact, that I have no possibility to show the MasterPage, even no Button is shown.
I have tried the solution of th linked question, but I get the error: "Object Element sap.m.Page#__page0 has no method 'getView'". So it didn't work.
Even a Do-it-yourselve solution, adding a button in a custom-Header:
contentLeft: [new sap.m.Button({
icon: "sap-icon://paging",
press: function() {
var splitApp = sap.ui.getCore().byId("ContentApp");
if (splitApp.isMasterShown()) {
console.log("Master weg");
splitApp.hideMaster();
} else {
console.log("Master kommt");
splitApp.showMaster();
var oSplitApp = this.getView().getParent().getParent();
var oMaster = oSplitApp.getMasterPages()[0];
oSplitApp.toMaster(oMaster, "flip");
};
}
})]
and trying to show the Master manually didn't work.
The SAPUI5 function: oSplitApp.showMaster(); works fine in the explorer but not on the device! Does anybody knows why? And has probably a solution?
I've found it out:
Even with the postet "tricks", the SplittApp class is not available for the Phone category. So, to see the masterView, I have created a PopUpMenu, which looks like the MasterPage.
Everything fine know and thank you for downgrading my question, because nobody knows that exactly.

Get the string from selected text/ highlighted text using JXA

I'm supper new here, either Javascript and JXA, so pardon me if I make some stupid questions. But I'm trying to figure out a way to get the string from the highlighted text using JXA - JavaScript for Automation, for Javascript can be recognized in Automator since Yosemite, I thought I can make something work with these:
window.getSelection in:
function getSelectedText() {
if (window.getSelection) {
txt = window.getSelection();
} else if (window.document.getSelection) {
txt =window.document.getSelection();
} else if (window.document.selection) {
txt = window.document.selection.createRange().text;
}
return txt;
}
This code is not mine, somebody posted this. But I've found out that I can't use window or document here in Automator to make change to Mac OS, so can someone show me how to convert this Javascript code into JXA which Automator can understand?
Thanks a lot!
In general, you can use the System Events app to copy and paste with any app.
'use strict';
//--- GET A REF TO CURRENT APP WITH STD ADDITONS ---
var app = Application.currentApplication()
app.includeStandardAdditions = true
var seApp = Application('System Events')
//--- Set the Clipboard so we can test for no selection ---
app.setTheClipboardTo("[NONE]")
//--- Activate the App to COPY the Selection ---
var safariApp = Application("Safari")
safariApp.activate()
delay(0.2) // adjust the delay as needed
//--- Issue the COPY Command ---
seApp.keystroke('c', { using: 'command down' }) // Press ⌘C
delay(0.2) // adjust the delay as needed
//--- Get the Text on the Clipboard ---
var clipStr = app.theClipboard()
console.log(clipStr)
//--- Display Alert if NO Selection was Made ---
if (clipStr === "[NONE]") {
var msgStr = "NO Selection was made"
console.log(msgStr)
app.activate()
app.displayAlert(msgStr)
}
For more info see:
Sending Keystrokes in JXA
JXA Resources
You need to mix JXA and Safari’s javaScript…
var Safari = Application("Safari") // get Safari
selection = Safari.doJavaScript("document.getSelection().toString()",{
in: Safari.windows[0].tabs[0] // assume frontmost window and tab
})
The script is in JXA, but the document.getSelection().toString() is Safari’s javaScript.
Of course you will need to enable apple events in Safari… http://osxdaily.com/2011/11/03/enable-the-develop-menu-in-safari/
If you want the selected text from another application, the code might be very different.
Don't do that, it's only applicable to JavaScript embedded inside a web browser. JXA is a standalone JS interpreter that has absolutely no understanding of web pages or DOM (and frankly doesn't have much clue about Mac application scripting either, btw).
Instead, use Automator to create an OS X Service as services can manipulate selected text in almost any OS X app; no application scripting required.

Open URL with a set Target

EDIT:
The solution I am seeking is a command line to be run to accomplish this from a batch file in Windows.
How would I mimic a browser function to open a URL with a specific target so that if that tab is already open it will load there instead of creating a new tab?
So instead of the default http://url.com with the target "_blank", I could change this to "w00t", etc.
I am using Chrome exclusively, so if this were to be a Chrome specific command, that would be acceptable.
You could create a HTML-page and add the following code like this:
<script>
var url = getQueryVariable("url");
var target = getQueryVariable("target");
window.open(url,target);
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
}
</script>
I have this script hosted here: scriptcoded.github.io/redirect
You could then call it from a batch-file using this command:
START scriptcoded.github.io/redirect?url=http://www.google.com&target=w00t
Use ?url=http://www.google.com&target=_blank to set the redirect.
The only problems I find with this method is that some browsers blocks the new window and that the tab won't close. Yes, we could use window.close();, but it will only close tabs that were opened by the webpage itself.
Hope it helps!
set name for html tag of target page like this:
<html name="w00t">
Note: A link to URL with target name, sets the name for target window automatically:
Google

Google Script to open URL in same window as Spreadsheet

I'm trying to work around the lack of API for Google Spreahsheet's Filter Views, by passing the filter view's URL into a hyperlink displayed in a sidebar.
Importantly: I want the filter view URL to open in the same window as, and thus replace, the spreadsheet. The hyperlink target should then be _self
function listFilterViews(){
var uiInstance = UiApp.createApplication()
.setTitle('Teacher Views');
var panel = uiInstance.createVerticalPanel();
panel.setSpacing(5)
var scroll = uiInstance.createScrollPanel();
scroll.setHeight("100%")
var url = "https://docs.google.com/blablabla"
var link = uiInstance.createAnchor("click me", url)
link.setTarget("_self")
panel.add(link);
scroll.add(panel)
uiInstance.add(scroll);
SpreadsheetApp.getUi().showSidebar(uiInstance);
}
However, the URL doesn't open in the same window as expected but in an other window instead. How can I fix this?
According to the setTarget documentation:
"(setting a target to _self) will only work if the app is being shown as a standalone service. It will have no effect when called inside a dialog, such as in a Google Spreadsheet."
Apps Script intentionally prevents scripts from being able to switch the view of the current window, as a security measure.

Open a browser window from an XUL application

How can I open a browser (i.e. Firefox, Chrome, IE, etc.) window from an XUL application?
I've tried the obvious window.open(url, "_blank") but the only effect this has is to close the current XUL window (!). A long trawl of Google hasn't produced anything fruitful either.
Just to make things clear, what I want is to open a URL in the user's default browser. I don't want to open the URL in my XUL application.
Not sure if this helps you, but i use this to open a new tab.
var win = Components.classes['#mozilla.org/appshell/window-mediator;1']
.getService(Components.interfaces.nsIWindowMediator)
.getMostRecentWindow('navigator:browser');
win.gBrowser.selectedTab = win.gBrowser.addTab(myUrl);
You need to set myURL to the page you want to open.
More Information here
Also note that this will surely not open IE or Chrome. I'm happy with that as long as it does not open IE:-)
This is what I do:
function OpenInDefaultBrowser(uri)
{
if(typeof uri == 'string')
{
var ioservice = Components.classes["#mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
uri = ioservice.newURI(uri, null, null);
}
// Open URL in user's default browser.
var extps = Components.classes["#mozilla.org/uriloader/external-protocol-service;1"]
.getService(Components.interfaces.nsIExternalProtocolService);
extps.loadURI(uri, null);
}

Resources