How to access added plugins in webdriver - javascript

I have added a plugin in chrome but, how can i access it through webdriver
File addonpath = new File("path of .crx file");
ChromeOptions chrome = new ChromeOptions();
chrome.addExtensions(addonpath);
WebDriver driver = new ChromeDriver(chrome );

Hi please do it like below
public class ChromeProfileWithAddOn {
public static void main(String[] args) {
// TODO Auto-generated method stub
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
// Add ChromeDriver-specific capabilities through ChromeOptions.
// i have added this add on on chrome = https://chrome.google.com/webstore/detail/pdf-converter/dmgnkfgleaamgbhhojkfijjmjmngokkb
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
########paste the location of .crx file you get in step 6 here########
options.addExtensions(new File("C:\\Users\\###\\Desktop\\hgmloofddffdnphfgcellkdfbfbjeloo.crx"));
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
System.getProperty("webdriver.chrome.driver","D:\\eclipseProject\\###\\src\\com\\###\\chromedriver_win32 (1)\\chromedriver.exe");
ChromeDriver driver = new ChromeDriver(capabilities);
// call chrome driver
driver.navigate().to("chrome-extension://ldlmdngominhfffemgnfpoifladkpple/RestClient.html");
How to get above url :
1.open the mainfest.json file there you will find "local_path":"RestClient.html"
2. make sure your extension is installed in chrome and then
3. go to chrome://extensions/ (follow steps-3 and 4 below)
4. you will get an ID value as shown in the image just below step 4 copy that
5. now u can make your url as "chrome-extension://ID/local_path"
6. now open it in chrome browser
} }
Code will be as above but we have to follow Some basic steps :
NOTE : i am talking example of Advanced REST client for ** CHROME** url below : https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo
Step 1 :
`Please download or if in-case already present go to the extension's manifest.json file`
if you want to download the add-on on your local drive please follow like this :
a.> download this extension https://chrome.google.com/webstore/detail/chrome-extension-source-v/jifpbeccnghkjeaalbbjmodiffmgedin
b.> install it (on top right hand side of the chrome browser a CRX button will appear)
c.> Now search Advanced REST client in the chrome web Store or simply copy and paste the link in the browser
https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo
d.> Click on the CRX image/button, you will get two options
Select download as zip (save it at your preferred location).
e.> unzip it
f.> inside unzipped folder you will find manifest.json file
Step 2 :
Copy the location of mainfest.json file
Step 3 : go to Chrome browser and in the url please type
chrome://extensions/
here all installed add on/extensions will be shown
Step 4 :
please check the Developer option
option for Pack Extension will be visible now
Step 5 :
Click Pack Extension and under Extension root directory:(First option) paste the location of manifest.json file.
Step 6 : if u have followed everything as above u will get this
Hope this solves your query.

Related

How can an image be saved with JavaScript for Automator (JXA) in macOS Preview application?

There is an image open in the Preview application, and it is unsaved.
I have tried this code, by building and running a standalone script in Script Editor:
var preview = Application('Preview')
var preview.documents[0].close({ saving: true, savingIn: '/Volumes/USB Drive/Untitled-Image.png' })
I see this in the logs
app = Application("Preview")
app.documents.at(0).Symbol.toPrimitive()
--> Error -1700: Can't convert types.
app.documents.at(0).Symbol.toPrimitive()
--> Error -1700: Can't convert types.
However, I'm not sure I'm reading the docs correctly, and I'm not sure how to further debug this code. A satisfactory alternative here would be to send a keystroke to save and click the Save button.
What can I do to persist the image in Preview to a file?
One can declare paths speculatively in JavaScript using Path('...'), and then use the save command to save the image to the file that will be created at that path:
Preview = Application('com.apple.Preview');
ImageFilepath = Path('/Users/CK/Pictures/Preview_SavedImage.jpg');
Preview.documents[0].save({ in: ImageFilepath });
This code worked for me.
var preview = Application('Preview')
var photo = preview.documents[0]
photo.close({ saving: 'yes', savingIn: Path("/Path/To Some/file_name.png") })
Upon execution, in the Replies log of Script Editor window, something slightly different from the question sample is logged.
app = Application("Preview")
app.close(app.documents.at(0), {savingIn:Path("/Path/To Some/file_name.png"), saving:"yes"})
I was able to figure this out by studying the scripting dictionary for macOS Mojave 10.14 as well as the Release Notes for JXA; there is a similar example under Passing Event Modifiers section of that.
From the Script Editor scripting dictionaries, this is the entry for the close method in the Standard Suite:
close method : Close a document.
close specifier : the document(s) or window(s) to close.
[saving: "yes"/‌"no"/‌"ask"] : Should changes be saved before closing?
[savingIn: File] : The file in which to save the document, if so.
Note the key differences between the working code here and the code in the question:
Specify a Path object when passing the savingIn option to close.
Specify a the saving option as one of "yes", "no", or "ask" (not true).
If you're new to JXA, Automator, and/or Script Editor for Mac, checkout the JXA Cookbook.
If this answer helps you, you're probably having a bad time, so best of luck!

Launch .exe with html link [firefox]

I am currently developing an application in php / js / html and I would like to launch an application made in C # via a html button (no download) however I use Mozilla Firefox and the only solution I found is a script featuring ActiveXObject which is only used by IE apparently.
You can use the Firefox command line options of -url
"C:\Program Files\Mozilla Firefox\firefox.exe" -url "https:\\www.stackoverflow.com"
or
Use selenium Webdriver, this can launch the browser to any url you specify and can fill any form on the page with any details you specify and press any buttons that you can find.
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using SeleniumExtras.WaitHelpers;
class HelloSelenium
{
static void Main()
{
using (IWebDriver driver = new FirefoxDriver())
{
WebDriverWait wait = new WebDriverWait(driver);
driver.Navigate().GoToUrl("https://www.stackoverflow.com/ncr");
driver.FindElement(By.Name("q")).SendKeys("Vote Up" + Keys.Enter);
}
}
}

ReferenceError while using sdk/tabs in firefox webextension

This is my first time learning to build a firefox addon. I want store all the open tabs in a window and for that I require sdk/tabs.
Here is my js file:
/*
Given the name of a beast, get the URL to the corresponding image.
*/
debugger;
var tabs = require("sdk/tabs");
function beastNameToURL(beastName) {
switch (beastName) {
case "Save Session":
debugger;
for (let tab of tabs)
console.log(tab.url);
return;
case "Load Session":
debugger;
return chrome.extension.getURL("beasts/snake.jpg");
case "Turtle":
return chrome.extension.getURL("beasts/turtle.jpg");
}
}
/*
Listen for clicks in the popup.
If the click is not on one of the beasts, return early.
Otherwise, the text content of the node is the name of the beast we want.
Inject the "beastify.js" content script in the active tab.
Then get the active tab and send "beastify.js" a message
containing the URL to the chosen beast's image.
*/
document.addEventListener("click", function(e) {
if (!e.target.classList.contains("btn")) {
return;
}
var chosenBeast = e.target.textContent;
var chosenBeastURL = beastNameToURL(chosenBeast);
chrome.tabs.executeScript(null, {
file: "/content_scripts/beastify.js"
});
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {beastURL: chosenBeastURL});
});
});
When I reach the var tabs = require("sdk/tabs") line I get a Reference error.
Github : https://github.com/sagar-shah/Session-manifest
Kindly let me know how do I resolve this error. This being my first time with add-ons I am completely lost.
Thanks in advance.
Update:
Tried to declare it globally in the js file. Now I am getting undefined error for tabs.
Update2:
I was mixing up development using sdk and webextensions as pointed out by #matagus. I have decided to go with development using the webextensions. Link to the new repository has been updated.
The error is on package.json line 6: you're telling to the addon sdk that the main file of your addon is manage.json. According to [the docs] the value of main should be:
A string representing the name of a program module that is located in one of the top-level module directories specified by lib. Defaults to "index.js".
So you need to change its value to index.js.
Besides that, I think you're missing a difference between Firefox addon built using the addon-sdk (which do not have a ´manifest.json´ and that you build using jpm tool) and the new WebExtensions which do require you to write a ´manifest.json´ like the one already have.
UPDATE:
Again: you're missing the difference between WebExtensions and SDK-based addons. Now you made a WebExtension but you're trying to use the SDK. It isn't possible. Just use chrome.tabs directly instead of trying to import it from the sdk (var tabs = require("sdk/tabs");).

Firefox Add-on SDK : equivalent to Chrome "chrome_url_overrides"

I'm making add-on modules for several browsers, including Chrome and Firefox.
I just did what I want with Chrome : overriding new tab url to load a HTML file with the manifest.json. Pretty simple. (Now I'm able to copy paste again :)) :
...
"chrome_url_overrides" : {
"newtab": "override.html"
},
...
When the user create a new blank tab, it overrides the default page and loads the file I want : override.html, with the same behavior (no URL in the address bar).
But now, I want to do the same thing with Firefox using Add-on SDK. I tried using browser.newtab.url :
var { get, set } = require("sdk/preferences/service");
var { when: unload } = require("sdk/system/unload");
var oldValue = get("browser.newtab.url");
set("browser.newtab.url", 'http://www.example.com');
// Restore old setting when unload
unload(function() {
set("browser.urlbar.autoFill", oldValue);
});
It works, but I really would be able to load an HTML file instead giving it a new URL (the code would be more maintainable) and keep a clean address bar.
Any ideas ?

Create XPI package with the Add-on SDK?

I got task to write an add-on for Firefox which will add an div element to existing page. I downloaded Add-on SDK and wrote a main.js file that looks like this:
var data = require("sdk/self").data;
require("sdk/tabs").on("ready", ExecuteAd);
function ExecuteAd(tab) {
if ( tab.url.indexOf("some url checking") > -1 ) {
var image = "http://www.lavasoft.com/img/product_icons/aaw11/free.png";
var link = "http://www.google.me";
tab.attach({
contentScriptFile: data.url("myscript.js"),
contentScript: "appendFunc('"+image+"', '"+link+"');"
//contentScript: "alert('Works');"
});
}
}
When I execute command cfx run it starts Firefox and if I go to specific web pages this script works. But when I create XPI file with cfx xpi and then click on Firefox and open that file it installs my add-on but now when I go to same web pages I gave been before add-on does not work. I have this external Javascript file which is stored in folder 'data'.
appendFunc is in myscript.js file.
How to make my extension work in production environment not just testing environment? I think that main problem is that it does not find this data/myscript.js (does it include in .xpi file?)
Don't mix contentScript and contentScriptFile. Also, you cannot know what of both is loaded first.
Instead load your script, and communicate using port.
main.js
var data = require("sdk/self").data;
require("sdk/tabs").on("ready", ExecuteAd);
function ExecuteAd(tab) {
var image = "http://www.lavasoft.com/img/product_icons/aaw11/free.png";
var link = "http://www.google.me";
var worker = tab.attach({
contentScriptFile: data.url("myscript.js")
});
worker.port.emit("showAd", {image: image, link: link});
}
myscript.js
self.port.on("showAd", function(data) {
console.log("showing ad", data.link, data.image);
});
Also, it sounds like PageMod would be a better choice for what you're doing.
PS: Also consult the Add-on Policies if you're planning to host on the addons.mozilla.org website. The policies e.g. prohibit injecting ads that a) aren't clearly marked as such and b) where the user did not opt-in prior to that.

Categories