How do I use Chrome webrequest API on an extension? - javascript

Alright, Im fairly new to making chrome extensions in general and I want to know how to intercept a tag and make a site run a different javascript file when the script is called. I have an extension made to test this out and try to figure it out myself, but it has not worked. When I type in the url of the file to replace, it redirects me to the file that I want it to, but the tag won't redirect to it or if it is, it's not running any of the code from the file. Please help?
content.js
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
if( details.url == "http://www.bellum.io/js/attackCooldown.js" )
return {redirectUrl: "http://yourjavascript.com/1177311921/alertjs.js" };
},
{urls: ["*://www.bellum.io/*.js"]},
["blocking"]);
manifest.json
{
"name": "Simple Webrequest Test",
"version": "0.1",
"description": "Webrequest Test",
"manifest_version": 2,
"permissions": [
"<all_urls>","webRequest","webRequestBlocking","http://*/*"
],
"background": {
"scripts": ["content.js"],
"persistent": true
}
}

Related

How can I call a Powershell / Command Line Script using a BrowserAction in a FireFox WebExtension?

When the button is pressed, I want to get the URL from the current tab and pass it as an argument to the script.
I know that I have to use browser.browserAction.onClicked.addListener() but I have not found an API for the call to the command line.
Can anybody tell me if this is possible and if not if there is a workaround?
Edit: Thanks to #wOxxOm who pointed me to Native messaging in the comment I was able to proceed as follows:
I used the example here to create my extension and app manifest.
Extension manifest:
manifest.json
{
"manifest_version": 2,
"name": "test",
"version": "1.0",
"description": "Description",
"icons": {
"48": "icons/test_logo-48.png"
},
"browser_action": {
"default_icon": "icons/test_logo-48.png",
"default_title": "test"
},
"browser_specific_settings": {
"gecko": {
"id": "test#example.org",
"strict_min_version": "50.0"
}
},
"background": {
"scripts": ["background.js"]
},
"permissions": ["nativeMessaging"]
}
App manifest:
native_manifest.json:
{
"name": "test",
"description": "Example host for native messaging",
"path": "C:\\Users\\test\\Documents\\PowerShell\\Scripts\\extension.bat",
"type": "stdio",
"allowed_extensions": [ "test#example.org" ]
}
Note that the path in the native_manifest.json is the path to a Windows .bat file which runs the Powershell script
I created two registry entries:
New-Item -Path HKCU:\SOFTWARE\Mozilla\NativeMessagingHosts\test -Value "C:\\Users\\test\\Documents\\PowerShell\\firefox_extension\\test\\native_manifest.json"
New-Item -Path HKLM:\SOFTWARE\Mozilla\NativeMessagingHosts\test -Value "C:\\Users\\test\\Documents\\PowerShell\\firefox_extension\\test\\native_manifest.json"
These entries point to the location of the App manifest. This is the root folder of the extension where the manifest.json and the background.js is also saved.
The background.js extension script:
/*
On startup, connect to the nhative app.
*/
let port = browser.runtime.connectNative("test");
On a click on the browser action, send the app a message.
*/
browser.browserAction.onClicked.addListener(() => {
console.log("Sending: ping");
port.postMessage("example.txt");
});
The .bat file launching the script:
extension.bat
#echo off
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '.\fire.ps1'"
Powershell test script:
param ($test="C:\Users\test\Documents\PowerShell\firefox_extension\test\default.txt")
get-date | out-file $test
The script works, the test file is created, but with the default parameter path. So far so good, but I cannot figure out how to read the passed message in Powershell from Stdin.
I found an answer here explaining to use the -command - parameter, however I'm already using it to call my script. Another possibility seems to be to use [Console]::In but I'm not sure how.
Can anybody help and provide an example how I can read the passed message from the extension in the powershell script? According to documentation it is passed to stdin.

How to know whether turn on sync in chrome browser is enabled using javascript

I am trying to check below point for my assignment programatically
1. check if turn on sync is enabled in chrome
EDIT:
So far i have developed a chrome extension and trying to get the chrome.storage.sync object
But i am unable to get the status of the turn on sync "whether synced or no"
Below is my manifest.json
{
"name": "Check Turn on sync",
"version": "1.0",
"description": "Build an Extension!",
"permissions": ["storage"],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"manifest_version": 2
}
I have provided a background.js file
is there a way in chrome.storage.sync that provides status of the sync
I am going to answer my own question, as i was able to find the answer.
I was able to get the status of sync is turned on or no along with the email ids. Here is the code :
create a manifest.json file with below content
{
"name": "Test turn on sync",
"version": "1.0",
"description": "Extension",
"permissions":[
"identity",
"identity.email"
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"manifest_version": 2
}
Create a background.js file in the same folder
chrome.identity.getProfileUserInfo(function(data) {
if (data.id) {
alert(data);
}
else
{
alert("User has not turned on sync");
}
});
Add the folder as chrome extension Run the extension from chrome with the help of identity.email in permissions, you will be able to get even the email-id of the user who has turned on sync, along with status.

Chrome extension - Detect URL and execute an application

I know that with a basic chrome extension we can select domains where our extension will work (<all_urls>), but is this possible to execute an application ( internal ) when the user visit a specific domain?
I looked previously and saw "Native Messaging". ( I'm currently studying arithmetics ) I want my extension to open automaticly calc.exe when I'm on my working lab's website.
I already did this:
manifest.json
{
"name": "Mon extension",
"version": "0.0.1",
"background": {
"scripts": [ "background.js" ]
},
"browser_action": {
"default_title": "Ouvrir la calculatrice"
},
"permissions": [
"nativeMessaging"
],
"manifest_version": 2
}
But I did not understand how to manage the javascript part, can someone explain it to me ?
My calc.bat's manifest.json :
{
"name": "application",
"description": "Lauching my app",
"path": "C:\\Users\\root\\Documents\\calc.bat",
"type": "stdio",
"allowed_origins": [
"chrome-extension://blfgmcilkaooeokpomhcpnfnhppjklcb"
]
}
Calc.bat is simply: #echo off & start calc.exe
Thanks in advance. If my question is not clear enough, tell me, I'll modify it.
CouldnĀ“t get yours to work but the example I linked worked perfectly so started editing that one and ended up with this :
Download sample host
Edit native-messaging-example-host.bat and replace with :
#echo off
calc.exe
Run install_host.bat
Make a folder called app on same directory as host folder
Create 3 files : background.js, main.js, manifest.json
The content is:
background.js:
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
if (message.action == "open app") {
chrome.runtime.connectNative("com.google.chrome.example.echo");
}
});
main.js:
chrome.runtime.sendMessage({action: "open app"}, function(response) {
console.log(response);
});
manifest.json:
{
// Extension ID: knldjmfmopnpolahpmmgbagdohdnhkik
"key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcBHwzDvyBQ6bDppkIs9MP4ksKqCMyXQ/A52JivHZKh4YO/9vJsT3oaYhSpDCE9RPocOEQvwsHsFReW2nUEc6OLLyoCFFxIb7KkLGsmfakkut/fFdNJYh0xOTbSN8YvLWcqph09XAY2Y/f0AL7vfO1cuCqtkMt8hFrBGWxDdf9CQIDAQAB",
"name": "Native Messaging Example",
"version": "1.0",
"manifest_version": 2,
"description": "Send a message to a native application.",
"content_scripts": [{
"js": ["main.js"],
"matches": ["<all_urls>"]
}],
"background": {
"scripts": ["background.js"]
},
"permissions": [
"nativeMessaging"
]
}
Finally, install the extension. This one in particular will open calc.exe every time you open/load a page, which can be easily changed with matches.
Another thing to note is that without the key in the manifest, it does not seem to work.

Chrome extension inject js

I want to create a new chrome extension but it don't work.
I want to inject a js file into web page (all web page,not only one.If i push the chrome icon on google the script must execute,if i push the icon on facebook it must execute ect.)
this is background.js
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript({
null,{file: "backgrounds.js"} });
});
this is backgrounds.js
document.body.innerHTML="display div elem with style and id";
this is manifest.json
{
"name": "MyExt",
"description": "an extension,what else?",
"version": "1.0",
"permissions": [
"activeTab"
],
"content_scripts": [
{
"matches": ["http://*/*"],
"js": ["background.js"]
}
],
"browser_action": {
"default_title": "myExt"
},
"manifest_version": 2
}
what i wrong?
I'm on windows 8.1 Update 1 with chrome last version
Your manifest is wrong: you should set background.js as your background script:
"background" : { "scripts" : [ "background.js" ] },
and remove the "content_scripts" section.
The "activeTab" permission means that you don't need to specify host permissions to inject in the current tab upon browser action click, so no other permissions are needed.
The tabId argument is optional, you can just drop it instead of passing null. And your invocation is wrong (you're wrapping two arguments in a single object). Here's the correct way:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript({file: "backgrounds.js"});
});

Chrome executeScript function not working

So I have an extension I'm writing and I'm trying to execute a script when the user clicks on the pageAction icon. When the icon is clicked, the method calls chrome.tabs.executeScript(...). The problem is that the chrome.tabs.executeScript function is not executing and I can't tell why. I know that I'm getting to the code where it calls executeScript because I have an alert there that appears. Here is some of my code:
manifest.json
{
"manifest_version": 2,
"name": "name here",
"description": "description here",
"version": "0.1",
"permissions": [
"<all_urls>",
"tabs"
],
"icons": {
"16" : "images/icon16.png",
"48" : "images/icon48.png",
"128": "images/icon128.png"
},
"background": {
"scripts": ["js/check.js"]
},
"page_action": {
"default_icon": {
"19": "images/icon19.png",
"38": "images/icon38.png"
},
"default_title": "default title here"
}
}
js/check.js
chrome.tabs.onUpdated.addListener(checkForValidUrl);
function checkForValidUrl(tabId, changeInfo, tab) {
if (tab.url.indexOf('g') > -1) {
chrome.pageAction.show(tabId);
}
};
chrome.pageAction.onClicked.addListener(function(tab) {
alert("hello world"); //this code is executed...
//...this code is not
chrome.tabs.executeScript(tab.id, {file: "save.js"}, function() {
if(chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
}
});
});
js/save.js
alert("hello world");
Like I say in the code, the hello world in my pageAction onClick function works. The executeScript method does not. Any idea about what is going on would be helpful.
After messing around with lots of different things in my code, I've found the solution to my problem. The error seems to be in the line that says {file: "save.js"}. When it's looking for save.js, it's apparently looking in the top directory, where my manifest.json file is located, not in the directory that my code is in. I had to change my code to {file: "js/save.js"} in order for my save.js file to be found.
According to the docs:
To insert code into a page, your extension must have cross-origin permissions for the page. It also must be able to use the chrome.tabs module. You can get both kinds of permission using the manifest file's permissions field.
So you need a permission for the site, i.e. http://example.com/ in the permission field.

Categories

Resources