Chrome extension background page restriction after event or setTimeout [duplicate] - google-chrome

I am trying to create a Chrome extension that when entering a certain site. I am deleting the download history. I am using this from the background page:
chrome.downloads.erase({},
function(item)
{
console.log("Erased item");
console.log(item);
});
The problem is that it is not working when I am sending a message from the contact script after entering the url I wanted.
I use send Message in the content script
chrome.runtime.sendMessage
and receive the message in the background page
chrome.runtime.onMessage.addListener
This is the output I get although there are some files in the download history:
Erased item
Array[0]
If I run the erase() from within the background page console it works fine
it seems that it happens because there is no user interaction.
If I put a clear button and click on it the deletion does works
But if I use timeout it does not
Any Idea why it happens?
I have seen that it has something to do with the setTimeout
This works in the background page:
setTimeout(function(){
console.log('erasing downloads');
chrome.downloads.erase({});
}
, 0);
But this does not:
setTimeout(function(){
console.log('erasing downloads');
chrome.downloads.erase({});
}
, 1000);

I think I had finally found the reason for the problem.
After downloading I have closed Chrome with taskkill /f, like this:
taskKill /im "chrome.exe" /f
After reopening Chrome I got in this line in the download page:
thefilenamedownloaded.exe The browser crashed before the download completed.
If I used taskKill /im "chrome.exe" (without /f) than the problem did not happen.
The bug may be actually more complicated than this because that when I had user interaction its also worked. But for now it solved my problem. Hopes this help anyone else

Related

Chrome devtools erroneously pauses on exception in ignored script

Image speaks for itself; this script is on the debugger's ignore list; yet every time I trigger this exception it is paused upon. I cannot uncheck Pause on caught exceptions because I am trying to pause on a caught exception in another startup script.
Devtools says The debugger will skip stepping through this script, and will not stop on exceptions, but it's doing just that, it is not skipping this script.
I've tried several things, like unignoring/reignoring. Using canary, etc. I have this problem on both my windows and osx machines; so it doesn't seem to be particularly related to my environment.
I am wondering if anyone else has run into this and found a workaround. Thank you.
Problem remains unsolved. Leaving this post here with some unsucessfull tentatives of mine, so someone can build something based on them.
1. Devtools extension API
It is possible to create an add-on that can reach the Developer Tools window and even reach the Sources tab, but once there all I could do was creating new sidepanels or attaching a listener to code text selection changes: chrome.devtools.panels.sources.onSelectionChanged.addListener((x)=>{console.log("onselectionchanged");console.dir(x);});
This API was not enough, could not reach debug status or any interesting sidepanel.
2. Debugging the debugger with a JS debugger
By hitting ctrl-shift-i or ctrl-shift-j over a devtools debug window it is possible to open another devtools debug window, debuging the first one. From there it is possible to write code that detects the banner informing that the file was supposed to be ignored and then click on the continue button:
function breakpointskipper() {
bnr = document.getElementById("sources-panel-sources-view").querySelector("div.vbox.flex-auto > div > div > div > div.flex-none > div");
if (!bnr) return;
bnr = bnr.shadowRoot;
if (!bnr) return;
bnr = bnr.querySelector("div");
if (bnr.ariaLabel != "This script is blackboxed in the debugger") return;
btn = document.querySelector("div.scripts-debug-toolbar.toolbar");
if (!btn) return;
btn = btn.shadowRoot;
if (!btn) return;
btn = btn.querySelector("div > button[aria-label=\"Resume script execution\"]");
if (!btn) return;
btn.click();
}
It is possible to even attach this breakpontskipper() button presser to an event in the devtools window and automate things, but as soon as you close the debugger being debugged window, it is all over and you have to recreate the code and reattach again. As said before, I wasn't able to make any add-on reach here.
3. Debugging the debugger with a native debugger
One last available option would be using GDB to debug the DevTools and change its behavior, in the chromium documentation it is shown that they have debug symbols available but I didn't try this approach.
Please try the troubleshooting help, and share some feedback. Also it would help greatly if you pasted some script here.
Please check if your script black boxed like here
Did you accidentally turn on - break on all exceptions see here
Force a hard refresh, i.e. clear you cache like here
Turn off all break points, then do a restore, try again.
More from ref. on chromium bug site
Update 1: Can you please verify/double check that your file to ignore is actually added to the ignore list.
If you know upfront which files to ignore, head over to Developer Tools > Settings (Cog Icon) > Ignore List. Then add a file name or regex pattern you wish to exclude.
You should see something like

What are some advanced troubleshooting approaches for message passing in Chrome Extensions?

I've got a Chrome Extension where the popup messages the content script - pretty straightforward. The following works consistently on macOS (Chrome 61.0.3164.100) but not on Windows 10 (Chrome 61.0.3163.100):
aSearch.js (popup)
function sendMessage(comment){
window.close(); //Close popup.
console.log("In send message: " + comment);
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {method: "insertComment", comment: comment}, function(response) {
// console.log(response.farewell);
});
});
}
Then content.js:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log('In content script after receiving request to insertcomment...');
if (request.method == 'insertComment'){
etc...
I can provide a lot more code...but the "console.log" does not fire even though background.js shows the related activity.
The aSearch.js is for an html popup that replaces the initial one...after you log in (not sure if this is important). This is accomplished in background.js:
chrome.browserAction.setPopup({ //Sets popup to last visited
popup: 'aSearch.html' // Open this html file within the popup.
});
Any guidance on what can make messaging break? Should I be taking a different approach for a constant (frequent) messaging that is used across many tabs, potentially?
Why would Chrome for Windows act differently?
Side note: I actually updated Chrome for Windows as I wrote this question...after the update suddenly messaging worked, for a little bit, then went back to dormant.
UPDATING QUESTION:
I added more code to the aSearch.js popup at the top...
I think I'm closing the popup before the message can be sent...and it's working sometimes (and on my macOS dev machine) because the computer is fast. On Windows, which is VM on my mac, not so fast...
Tomorrow I'll move the close inside the response from messaging...waiting for message to reply.
Is this plausible?
The issue here was, indeed, that you can't close a popup window before the message is successfully sent.
Duh.
The challenge, though, is that on my machines the message is sent successfully before the window closes. So I didn't discover the bug.
I have the message receiver acknowledging success and then closing the popup. Works like a charm.

Chrome - type=file drag and drop

I have a weird situation. One of my users is using functionality of Chrome (current version, 49, 50), that he drags and drops a file from his folder on local computer to normal input type=file button in a form on the webpage. Once again, let me stress it even more, its normal html input, no fancy javascript, no drag&drop events and handlers, nothing like this. It just takes the name of the dragged file and puts it into the input field, as like he selected it via "normal" way, opening the file select window, locating file on harddrive, selecting the file and confirming.
In some specific situation, this stops working (while doing some edits in the page via javascript / ajax), and I need to "reenable" it.
But, and that is my question, I haven't found any documentation of this "feature" in Google Chrome (or maybe some other browsers as well, I don't know). Why it works, how it works, how it should work and what to do if it stops working :) Does anyone has any experience with this ? The only way how to "fix" it now is to reload the page. I'd love to solve it ... :)
EDIT 1 : I just did a quick test, it works and bugs the same way in Firefox on Win. It doesn't show any error in dev console or any message, it just doesn't add the file as expected.
I've found it. The previous discussion with deceze pointed me to test the javascripts I have on the page, that do not "interfere" with the input type=file ... they weren't any such scripts, but I've found that after doubleclick on the table (that I'm using for editing) this script is being called
$(document).bind('drop dragover', function (e) {
e.preventDefault();
});
and there wasn't any unbind action when table is saved ... this caused the drag and drop everywhere on the page to stop working after the edit.

How to stop chrome tab focus?

I am writing a chrome extension which refreshes pages after a certain time interval. I got it working fine, but a new problem occurred..
Whenever the page refreshes the tab gets highlighted (starts blinking) or else comes in focus. Ie it is getting focused on.
Now this works differently depending on the chrome browser state, but the core issue is the same:
1. If user switches tab to another one (suppose writing an email in gmail.com) while waiting for page to refresh itself, on refresh the current mail tab will get outfocused (so he wont be able to continue writing his email without clicking on the window first)
If the user swithces to any other application on his workspace (like my computer, outlook etc), on refresh the chrome icon will start blinking on the taskbar (highly annoying)
If the user has multiple chrome windows open with a tab each running the refresh code, then on refresh that window will get focus (come at top). This will repeat for all chrome windows whenever there specific refreshes occur.
Steps to reproduce the problem:
Write a simple extension
In the background page, create a new tab with:
chrome.tabs.create({ url: "https://drive.google.com/", active: false})
Now in a loop, after every minute, refresh this tab with:
chrome.tabs.update(tab_id, {url: "https://drive.google.com/",
active: false,
highlighted: false});
Once you start this code, either minimize the chrome window or shift your workspace and focus on something else.
Every time tabs.update() gets called, the Chrome window either unminimizes from task bar, or gets the focus on it.
What i want is to remove this focus whenever the page refreshes.
If anyone can please help me with this i would be very greatful.
Thanks,
Umair
EDIT:
code for my background.js file
chrome.webNavigation.onErrorOccurred.addListener(function (_errorDetails) {
var myUrl = 'myPage.htm';
chrome.tabs.update(_errorDetails.tabId, { url: myUrl, active: false,
highlighted: false, selected: false },
function (_tabDetails) {
chrome.windows.update(_tabDetails.windowId, { drawAttention: false });
});
});
Iv tried to make all related parameters false to stop focus.
Also on myPage.htm the function reload() is called on pageload.
function reloadPage() {
//code for 10 second delay
location.reload();
}
myPage.htm is itself a simple page showing few lines of text like 'Unable to load page' etc.
I changed the code and got it working for me as per my requirements.
What i did was instead of using the reload function to refresh page, i used the message passing technique of the chrome extension to send a message (containing url and tabID) from my js to background.js and update the relevant tab there with the url.
I know this is not a perfect solution, but it worked for me, therefore im sharing it.

How to detect when the chrome devtools is closed and trigger a function in background.js

I'm currently writing a chrome extension that has it's own panel in the Devtools. Although I'm facing an issue. I'm trying to trigger some action in the background page when the Devtools panel of a specific tab is closed.
I tried adding this to devtools.js :
window.onbeforeunload = function(event) {
port.postMessage({action: 'disableTab', tabId: tabID});
return "Are you sure you want to navigate away?";
};
Although the confirmation is never displayed and the message is never posted.
I think that it's because my devtools.html / devtools.js file isn't the one that is handling the main Devtools panel. it' only appended to it, something like that.
I'm still searching for an answer, anyone knows how I could achieve that ?
Edit:
I just noticed that this app is doing exactly what I want! https://developers.google.com/speed/pagespeed/insights_extensions
Run an analysis, then try closing the devtools, you'll be prompted.