Get url from google chrome tab - javascript

I'm building an extension for chrome, and I'm trying to pass the chrome tab url (with JS) dynamically to the html file. I read other people questions regarding the same issue, but it didn't work in my case.
My knowledge of JS and HTML is basic, but I don't know how to move the parameter data between them.
Thanks!

First, set the permissions for the tab API :
"permissions": [
"tabs"
]
And then store the URL :
chrome.tabs.getSelected(null,function(tab) {
var tablink = tab.url;
});
Or another way: (preferred)
chrome.tabs.query({'active': true}, function (tabs) {
var url = tabs[0].url;
});

Related

Google Chrome Extension:Call Python function from javascript

I am developing chrome extension and from my javascript file, I am getting the url of the active tab. Now I want to pass this url to my python function in python code. How can I achieve this. I have tried few solutions posted here, but its no help. I need to send the "myurl" in the below code to my python function and get back results too.
chrome.tabs.query({currentWindow: true, active: true}, function(tabs){
var myurl = tabs[0].url;
console.log(myurl);
});
Try this, might works
System.Diagnostics.Process.Start("python.exe", "file.py");

how to dynamically change chrome extension icon

I am developing a chrome extension. The scenario is kind of
When i click on the icon extension send POST request to server and on basis of the GET response it procceds on any of 3 different if/else if/ else statement. I am using page action to show the icon next to address bar.
I want my extension icon to change dynamically on each if/else if /else statement.
this is my backgound.js to make the icon visible next to address bar.
chrome.runtime.onInstalled.addListener(function() {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([
{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
})
],
actions: [ new chrome.declarativeContent.ShowPageAction() ]
}
]);
});
});
this is my manifest.json
"page_action" :
{
"default_icon" : "icon-191.png",
"default_title" : "xxx",
"default_popup": "popup.html"
},
Any suggestion how can i change extension toolber icon dynamically on diffetent statement?
Thanks In Advance!
Well, it's there in the docs.
declarativeContent API can only execute a very limited number of actions instead of arbitrary code.
Thankfully for you, chrome.declarativeContent.setIcon is an action that does exactly what you need. Use it just like the one you're using already, except it expects a parameter.
And give that docs page a read in general.

Can we download a webpage completely with chrome.downloads.download? (Google Chrome Extension)

I want to save a wabpage completely from my Google Chrome extension.
I added "downloads", "<all_urls>" permissions and confirmed that the following code save the Google page to google.html.
chrome.downloads.download(
{ url: "http://www.google.com",
filename: "google.html" },
function (x) { console.log(x); })
However, this code only saves the html file.
Stylesheets, scripts and images are not be saved.
I want to save the webpage completely, as if I save the page with the dialog, selecting Format: Webpage, Complete.
I looked into the document but I couldn't find a way.
So my question is: how can I download a webpage completely from an extension using the api(s) of Google Chrome?
The downloads API downloads a single resource only. If you want to save a complete web page, then you can first open the web page, then export it as MHTML using chrome.pageCapture.saveAsMHTML, create a blob:-URL for the exported Blob using URL.createObjectURL and finally save this URL using the chrome.downloads.download API.
The pageCapture API requires a valid tabId. For instance:
// Create new tab, wait until it is loaded and save the page
chrome.tabs.create({
url: 'http://example.com'
}, function(tab) {
chrome.tabs.onUpdated.addListener(function func(tabId, changeInfo) {
if (tabId == tab.id && changeInfo.status == 'complete') {
chrome.tabs.onUpdated.removeListener(func);
savePage(tabId);
}
});
});
function savePage(tabId) {
chrome.pageCapture.saveAsMHTML({
tabId: tabId
}, function(blob) {
var url = URL.createObjectURL(blob);
// Optional: chrome.tabs.remove(tabId); // to close the tab
chrome.downloads.download({
url: url,
filename: 'whatever.mhtml'
});
});
}
To try out, put the previous code in background.js,
add the permissions to manifest.json (as shown below) and reload the extension. Then example.com will be opened, and the web page will be saved as a self-contained MHTML file.
{
"name": "Save full web page",
"version": "1",
"manifest_version": 2,
"background": {
"scripts": ["background.js"]
},
"permissions": [
"pageCapture",
"downloads"
]
}
No, it does not download for you all files: images, js, css etc.
You should use tools like HTTRACK.

How do I get this very simple pageAction Chrome Extension ported to run on Firefox and Safari

This is a very simple Chrome Extension that uses pageAction. It evaluates the current URL, places an icon in the address bar if the URL matches a condition (location, really) and changes the URL (using a bit from the original location) to a new location when the user clicks the icon.
This was simple and straightforward to build the Chrome Extension. The docs are simple and Google provide some code examples that could be adapted and built upon. Finally, the CWS is easy to deploy to and from.
However, I have no experience whatsoever trying to do the same in FF or Safari.
Can someone please give me some guidance with some code examples and packaging advice?
Thanks!
Background.js
function checkForValidUrl(d, c, e) {
if (c.status === "loading") {
var b = e.url.split("/")[2];
var a = e.url.split("/")[3];
if (b === "www.somewhere.com" && a === "unfiled") {
chrome.pageAction.show(d)
}
}
}
chrome.tabs.onUpdated.addListener(checkForValidUrl);
chrome.pageAction.onClicked.addListener(function (b) {
var a = b.url.split("/")[4].split("+").slice(0, 1);
chrome.tabs.update(b.id, {
url: "http://www.somewhere.com/filed/" + a
})
});
Manifest
{
"name": "MyExtension",
"version": "1.0",
"description": "This is nifty",
"background": { "scripts": ["background.js"] },
"page_action" :
{
"default_icon" : "icon-19.png",
"default_title" : "Click to do your stuff"
},
"permissions" : [
"tabs"
],
"icons" : {
"48" : "icon-48.png",
"128" : "icon-128.png"
},
"manifest_version": 2
}
This answer is for Safari only.
For a Safari version of this extension, the raw materials you will need are:
a "global" HTML page (Safari's equivalent of a Chrome extension's background page)
a script that runs on the global page, provided either as an inline <script> or as a .js file
an icon for the toolbar button, called a "toolbar item" in Safari extension parlance
After creating a new, empty extension with Extension Builder, create the aforementioned files in, or move them into, the extension's folder. In Extension Builder, select your global page. Create a toolbar item, give it a label and an identifier, and select the toolbar button image. You'll also need to specify a command for the toolbar item; just enter any string, like "munge-url".
In your global script, you will add a listener for the "command" event, which Safari will send to the global page when the user clicks your toolbar button. The listening function will read the URL of the current tab, munge it, and set the URL of the tab to the munged one. Like this:
safari.application.addEventListener('command', function (evt) {
if (evt.command == 'munge-url') {
var currentTab = safari.application.activeBrowserWindow.activeTab;
var oldUrl = currentTab.url;
var newUrl = mungeUrl(oldUrl);
currentTab.url = newUrl;
}
}, false);
The mungeUrl function needs to be defined, of course.
That should be about it. If you want to get fancy, you can add code that will disable or enable the toolbar button based on the URL of the current tab; for that you will need a listener for the "validate" event, which is discussed on this page of the Safari Extensions Development Guide.

Difficulties saving URL from Chrome tab (for Chrome extension)

I'm attempting to build a Chrome extension which will check (through a "link:URL" Google search) which sites link to the one that is currently open in the active tab. But my code fails to properly save the tab's URL into a variable. I have found similar questions (and their answers) here on stackoverflow and I understand it may have sth to do with the fact that js is asynchronous, but I wasn't able to make it work. Any hint(s) would be hugely appreciated. Thanks!
// this is the part that doesn't work
chrome.tabs.query({'active': true}, function (tabs) {
var query = tabs[0].url;
});
// this is the part that works just fine
chrome.browserAction.onClicked.addListener(function(activeTab)
{
var stemURL = "http://www.google.com/#q=link:";
chrome.tabs.create({ url: (stemURL + query) });
});
Here's how I set the permissions in the manifest, which should be right
"permissions": [
"tabs", "http://*/*", "https://*/*"
],
This is because when you declare your "query" variable, it only has scope within your callback function in chrome.tabs.query. Now, you could declare your variable at the top of your script to give it scope within the entire script:
var query;
chrome.tabs.query({'active': true}, function (tabs) {
query = tabs[0].url;
});
chrome.browserAction.onClicked.addListener(function(activeTab)
{
var stemURL = "http://www.google.com/#q=link:";
chrome.tabs.create({ url: (stemURL + query) });
});
...or you could use your activeTab variable that chrome.browserAction.onClick passes to you and grab the active tab's URL from that to ensure that you open the search results for the tab that the user was viewing when they clicked your extension's button, which would also have the added advantage of cutting out the first half of your code:
chrome.browserAction.onClicked.addListener(function(activeTab)
{
var stemURL = "http://www.google.com/#q=link:";
chrome.tabs.create({ url: (stemURL + activeTab.url) });
});
I would recommend the latter approach; it's faster, cleaner, and more reliable. Good luck!

Categories

Resources