Google Chrome Extension:Call Python function from javascript - 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");

Related

How to save input data form with javascript avoid security content policy -Chrome extension

i'm newbie on developping chrome extension and i'm facing a problem.
I need to get user input data and store it in some ways. I tryed with local.storage and sync.storage but they should work only with same domain since i cannot get data while i'm trying to inject javascript by using script in content.js file. It works on same domain (i suppose that in this case, when i open tab of chrome extension, local.storage save data only for that "domain").
How can solve it?
Thank you
You can try the message passing.
// popup.js
function submit(data) {
chrome.tabs.query({active: true}, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, data);
});
}
// content.js
chrome.runtime.onMessage.addListener(function (message) {
// do sth
});

Why is chrome.tabs.query() only allowing me to update some tabs

I have been fiddling around with chrome extensions for a few days now to learn more about how they work. I am trying to make a simple extension that adds "/about" to the end of the url for every webpage. This bit of code works for several pages, such as chrome://extensions, chrome://settings, etc., but does not work for others, such as https://www.google.com, https://www.amazon.com, etc. Can anyone point to the problem with my code?
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
chrome.tabs.query({active: true, currentWindow: true, status: "complete"}, function(array_of_Tabs) {
var activeTab = array_of_Tabs[0];
var url = activeTab.url;
chrome.tabs.update({url: url + "/about"});
window.close();
});
});
I apologize for any bad code, I am just getting started learning javascript. Thanks!

Google chrome extension error when debugger open only

I am trying to send a ajax request to a server inside a google chrome extension. I am using angular with it too and the code is inside the controller. The request works fine generally, but when I have the the DevTools open it throws an error. This is the relevant part of the code:
$scope.sendLink = function (){
chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) {
var url = tabs[0].url;
var date = new Date();
var xPost = new XMLHttpRequest();
xPost.open("POST", **URL HERE*",true);
xPost.setRequestHeader("Content-type", "application/json");
xPost.send(JSON.stringify({"name":$scope.name,"url":url,"date":date}));
});
}
When I hit inspect element on the extension, and then do the request, the tabs array come back as empty. It works fine if the devTools window is closed. I can't figure out why that is the case. Any explanation would be appreciated!
You could have changed the javascript engine behavior via chrome-dev-tools. This would get activated only when the dev-tools are open.
For instance, once I found that a mate of mine had turned-off javascript using the chrome-dev-tools. His app wasn't executing when the chrome-dev-tools were open...

Get url from google chrome tab

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;
});

Google Chrome Extension - Background.html function question

Is there anyway by adding to this javascript I can ingore anything after the .com/ .net/ .org/ etc for tab.url.
So if tab.url = examplesite.com/blabla/blabla.html it will replace tab.url with examplesite.com/ and ignore anything after it.
Here's my background.html script.
<script type="text/javascript">
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.getSelected(null,function(tab) {
chrome.tabs.create( { url: "http://www.mysite.com/index.php?q=" +tab.url } );
});
});
</script>
Or do I need to program this into mysite to strip the Url? I was wondering if it is possible with Javascript... (not my forte.)
Thank you for any help you may be able to give me.
Unfortunately there is not parseUri function built into javascript but you could build what you're asking for using regular expressions. An example of this can be found here:
http://blog.stevenlevithan.com/archives/parseuri
Also, I've never tried to access it from a Chrome extension, but I suspect you have access to the window.location variable which is an object that contains broken down parts of the current page's url. Trying console.log(window.location) and look at the content of the object.

Categories

Resources