Getting File Url When A File Is Going To Download - google-chrome

My Work So FAR
Manifest.json
{
"manifest_version": 2,
"name": "Extension For PyIDM",
"version": "0.1",
"description": " An Open Source Alternative Of IDM",
"icons": {
"16": "icons/favicon-16x16.png",
"48": "icons/android-icon-48x48.png",
"120": "icons/apple-icon-120x120.png"
},
"author" : ["Harshil Pandey","#PyIDM"],
"permissions": [
"<all_urls>",
"downloads"
],
"background": {
"scripts": ["background.js","pyidm_extension.js","webextension.js"]
}}
Background.js
chrome.runtime.onInstalled.addListener(function() {
//window.open("install.html")
window.open('install.html','height=480,width=640', false);
});
chrome.downloads.onCreated.addListener(function() {
var r = confirm("Do you Want To Download"+ File + " Using PyIDM");
if (r == true) {
var final = chrome.downloads.onCreated.finalUrl;
alert(final + " I Am working");
//alert("You pressed OK!");
} else {
//alert("You pressed Cancel!");
}
}
)
Problem
How to Get The Url OF The File That IS Going Downloads
And The File NAme
What i am using to know if a file is going to download: chrome.downloads.onCreated

Related

Cannot read property 'onBoundsChanged' of undefined

When I write a extension for google chrome I got error as title question. Below is my code:
chrome.browserAction.onClicked.addListener(function (tab) {
chrome.tabs.executeScript(null, {
file: "libs/jquery.js"
}, function () {
chrome.tabs.executeScript(null, {
file: "src/content.js"
});
});
});
chrome.app.window.onBoundsChanged.addListener(function (tab) {
chrome.tabs.executeScript(null, {
file: "libs/jquery.js"
}, function () {
chrome.tabs.executeScript(null, {
file: "src/content.js"
});
});
});
Firstly, I got success with onClicked event. But I want extension work when I resized window. But when I add 2nd event. Error occur
Please help me this error
Thank you
manifest.json
{
"name": "Bootstrap Grid for any Website",
"version": "1.1.0",
"manifest_version": 2,
"description": "Quickly toggle a Bootstrap Grid for any website. Easy to use, easy success.",
"browser_action": {
"default_icon": "src/icon48.png"
},
"background" : {
"scripts" : [
"src/background.js"
]
},
"content_scripts": [
{
"js": [ "src/send_value.js" ],
"matches": [ "<all_urls>" ],
"all_frames": true,
"run_at": "document_end"
}
],
"permissions": ["activeTab"]
}
I think you've mixed up chrome extensions and chrome apps. As we can read here, chrome.app.window is
not associated with any Chrome browser windows
I recommend you create content.js script where you put something like this:
window.addEventListener('resize', function(e){
....
// here you send message to background.js
var message = { info: "window resized");
chrome.runtime.sendMessage( "", message );
})
content.js must be executed in every tab you specified in manifest file.
In your background.js you should write:
chrome.runtime.onMessage.addListener(function(message){
if(message.info === "window resized"){
chrome.tabs.executeScript(null, // ..... the stuff you've written above
......
}
});
Links:
chrome.runtime.sendMessage
chrome.runtime.onMessage
Good luck!
UPD (in manifest file):
"background": {
"scripts": [ "background.js"]
},
"content_scripts": [
{
"js": [ "content.js" ],
"matches": [ "<all_urls>" ],
"all_frames": true,
"run_at": "document_end"
}
],
UPD 2. This snippet is from my working extension manifest:
"web_accessible_resources": [
"main.js",
"style.css",
"options/options.css",
"options/options.js"
]
Your extension folder look like:
Your manifest.json file:
{
"name": "Bootstrap Grid for any Website",
"version": "1.1.0",
"manifest_version": 2,
"description": "Quickly toggle a Bootstrap Grid for any website. Easy to use, easy success.",
"browser_action": {
"default_icon": "src/icon48.png"
},
"background" : {
"scripts" : [
"background.js"
]
},
"permissions": [
"tabs",
"<all_urls>"
],
"web_accessible_resources":[
"src/content",
"src/content_temp",
"libs/jquery.js"
]
}
background.js:
chrome.browserAction.onClicked.addListener(function (tab) {
chrome.tabs.executeScript(null, {
file: "libs/jquery.js"
}, function () {
chrome.tabs.executeScript(null, {
file: "src/content.js"
});
});
});
chrome.runtime.onMessage.addListener(function(message){
if(message.info !== 1) return;
chrome.tabs.executeScript(null, {
file: "src/content_temp.js"
});
});
And your send_value.js file I put into content.js:
window.addEventListener('resize', function (e) {
// here you send message to background.js
var message = {info: 1}
chrome.runtime.sendMessage("", message);
});
function devTool() {
As for me, it works fine! :-)

"sync is not defined" error showing in chrome extension

sync is not defined" error showing in chrome extension while have used storage permission.please acknowledge me where is error and how
will solve message : "sync is not defined" stack : "ReferenceError:
sync is not defined↵ at eval (eval at
(http://localhost:46950/app/popup.js:31:9), :1:1)↵ at
HTMLInputElement.
(http://localhost:46950/app/popup.js:31:9)↵ at
HTMLInputElement.dispatch
(http://localhost:46950/app/img/jquery-3.2.1.min.js:6:12417)↵ at
HTMLInputElement.q.handle
//manifest.json
{
"manifest_version": 2,
"name": "One-click ",
"description": "This extension demonstrates a browser .",
"version": "1.0",
"app": {
"launch": {
"local_path": "popup.html"
}
},
"browser_action": {
"default_icon": "img/icon.png",
"default_popup": "popup.html"
},
"background": {
"scripts": [ "popup.js", "img/jquery-3.2.1.min.js" ],
"persistent": false
},
"content_scripts": [
{
"css": [ "img/bootstrap.min.css"],
"js": [ "app/popup.js", "img/jquery-3.2.1.min.js" ],
"matches": [ "http://*/*", "https://*/*" ]
}
],
"permissions": [
"activeTab",
"storage",
"tabs",
"identity"
]
}
//popup.js
chrome.storage.sync.set({ 'data':"helloset" }, function () {
alert("saved");
});
*method* is not defined errors usually means the method was being called while the library wasn't yet loaded or there's really no such method. In your case, I think it's the former. Looking at the IzumiSy/manifest.json sync example, you need to use document.body.onload first then call that function like:
// popup.js
document.body.onload = function() {
chrome.storage.sync.get("data", function(items) {
if (!chrome.runtime.error) {
console.log(items);
document.getElementById("data").innerText = items.data;
}
});
}
if you need to save data locally you can use
document.body.onload = function() {
chrome.storage.sync.get("data", function(items) {
if (!chrome.runtime.error) {
console.log(items);
document.getElementById("data").innerText = items.data;
}
});
}
document.getElementById("set").onclick = function() {
var d = document.getElementById("text").value;
chrome.storage.sync.set({ "data" : d }, function() {
if (chrome.runtime.error) {
console.log("Runtime error.");
}
});
window.close();
}

Getting data from user page in chrome extension

I am creating a chrome extension which extracts the inner text of h2> from the page in which the user is browsing. Although I learned about content scripts and have used it but it is not working I don't know where is the problem. If anyone could help me that would be greatly appreciated.
This is the js code:
document.addEventListener('DOMContentLoaded', function() {
var link = document.getElementById('button');
link.addEventListener('click', function() {
updateresult();
});
});
function updateresult() {
var i;
var x =document.getElementsByTagName("h2");
for(i=0; i< x.length ; i++)
{
document.getElementById("result").innerHTML = (x[i].innerHTML + "\n");
}}
This is the manifest file which I am using:
{
"manifest_version": 2,
"name": "Header Extractor",
"description": "Extension to search header",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"16": "icon.png",
"48": "icon.png",
"128": "icon.png",
"default_popup":"popup.html",
"default_title": "Open And Search headers"
},
"permissions": [
"storage",
"activeTab" ,"tabs"
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": [ "popup.js"],
"run_at": "document_end"
}
]
}
Have you already created a result div to paste your results in?
Also your current function only returns one h2 value. Let's use concat to output them all together.
function updateresult() {
var i;
var x = document.getElementsByTagName("h2");
var string = "";
for(i=0; i<x.length; i++)
{
string.concat(x[i].innerText + "\n");
}
document.getElementById("result").innerText = string;
}
As a side note, we use innerText instead.

Chrome never reads key NativeMessagingHosts to run native messaging

I have a chrome extension that I'm developing. Using process monitor, I'm filtering on NativeMessagingHosts in the path and it never gets hit on the registry. Here is my REG file:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\Software\Google\Chrome\NativeMessagingHosts\com.app.native.messaging.ChromeMessageRepeater]
#="C:\\Prototypes\\Plugins\\DG2\\com.app.native.messaging.ChromeMessageRepeater.json"
Now, here is the JSON for the plugin manifest:
{
"manifest_version": 2,
"name": "--------- 2.0",
"description": "",
"version": "2.0",
"content_scripts" :[
{
"matches": ["*://*/*"],
"run_at": "document_idle",
"js": ["jquery-1.12.2.min.js", "dg2.js"]
}
],
"background": {
"page": "back.html",
"persistent": true
},
"permissions": [ "nativeMessaging", "alarms", "tabs", "activeTab", "*://*/*"]
}
here is the json for the native messaging app
{
"name": "com.app.native.messaging.ChromeMessageRepeater",
"description": "",
"path": "ChromeMessageRepeater.exe",
"type": "stdio",
"allowed_origins": [
"chrome-extension://lpldkcbggjamnjbndpddgoikgeabfapb/"
],
"permissions": [
"nativeMessaging"
]
}
Any idea why the registry key is never read? When trying to use the plugin, the Port is never opened if I use chrome.runtime.connectNative nor if I used chrome.runtime.sendNativeMessage
EDIT:
function postMessage2(url, dom) {
var port = chrome.runtime.connectNative("com.app.native.messaging.ChromeMessageRepeater");
if(typeof chrome.runtime.lastError !== 'undefined') console.log(chrome.runtime.lastError.message);
port.onMessage.addListener(function(msg) {
console.log("Received" + msg);
});
port.onDisconnect.addListener(function() {
console.log("Disconnected");
});
try {
port.postMessage({ text: url + ';' + dom });
}
catch(ex) {
console.error(ex);
}
};
function postMessage(url, dom) {
chrome.runtime.sendNativeMessage("com.app.native.messaging.ChromeMessageRepeater", { text: url + ';' + dom });
if(typeof chrome.runtime.lastError !== 'undefined') {
console.log(chrome.runtime.lastError.message);
}
};
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if(request.type === "native"){
postMessage2(request.url, request.dom);
sendResponse({ result: true });
}
}
);

chrome.webRequest not working on Chrome 17

I have problems in chrome-extension API (chrome.webrequest).
My manifest.json
{
"name": "tesst",
"version": "1.0",
"description": "test",
"permissions": ["webRequest","webRequestBlocking",
"http://*" ],
"options_page": "options.html",
"background_page": "background.html"
}
My background.html
<script>
chrome.webRequest.onBeforeSendHeaders.addListener( function(info) { loldogs = [{name:'x', value: 'xx'}]; console.log("URL: " + info.url); info.requestHeaders.push.apply(info.requestHeaders,loldogs); for(var i in info.requestHeaders) { for (var key in info.requestHeaders[i]){ console.log("header "+i+" ["+key+"] "+info.requestHeaders[i][key]); } }
return {requestHeaders: info.requestHeaders}; }, {urls: ["<all_urls>"]}, ["blocking", "requestHeaders"]);
</script>
When I connect to some url (ex: http://google.com), chrome not set header {name:'x', value: 'xx'} in http-request. And it does not log anythings in console.
Change "http://*" to "http://*/*" in permissions.