0

I'm writing a basic start page chrome extension which features a search bar that should autofocus on page load. Now normally I would simply use the "autofocus" attribute on the input element in my HTML page. However, as Chrome extensions do not allow this or inline javascript in HTML pages of extensions, I've been forced to move my code outside to an external javascript file.

So my HTML looks something like this:

<form id="slot2form" method="get" action="http://www.google.com/search" target="_blank" onsubmit="closetab()">
            <input id="slot2input" name="q" type="text" class="search" placeholder="temporary"></input>
        </form>

Now closetab() (not important) and autofocus will work when the page is loaded on its own. However, these inline javascripts will not work when the page is loaded as a chrome extension. So, I've attached a javascript file called "listeners.js" which listens for events like pageload. Below is the code:

document.addEventListener("DOMContentLoaded", function(event) {
    $('#slot3').gCalFlow({
        calid: calConfig.calid,
        maxitem: 3
    });
    $('#slot2input').focus();
});

document.addEventListener("submit", function closetab() {
    setTimeout(function() {
        close();
    }, 1);
});

closetab() and the .gCalFlow sections work perfectly fine. However, I cannot for the life of me make the slot2input element obtain focus on the page load if the page is loaded as a chrome extension.

Are there any other ways I can obtain focus on this element, or am I simply missing something obvious?

Edit: I've tried eliminating other javascripts from the equation, and leaving just listeners.js in my testing. However, I still receive the same issue: no focus on DOMContentLoaded. I'm just about at a dead end here. Would my version of chrome or operating system matter at all?

Hawkins
  • 700
  • 6
  • 21
  • change the order of statement inside the top listener. make the focus statement to be second statement. I think focus is getting removed by the second statement. – Gyandeep Oct 27 '15 at 20:44
  • Just tried that; didn't appear to make any difference unfortunately. – Hawkins Oct 27 '15 at 20:47
  • I use jquery in my extension and it works fine. I would also remove autofocus attribute and then try it. – Gyandeep Oct 27 '15 at 20:48
  • I've removed autofocus and the dorky placeholder text, however the code still does not function as expected. I've replaced the javascript call with the appropriate jquery call. I'll update the main post in a moment to show. However, the input field is still not focused on page load – Hawkins Oct 27 '15 at 20:56
  • BTW here `autofocus` attribute works as it should without any additional js code. Can you post the entire popup.html and js (maybe as a link to pastie.org here in the comments if it's big)? – wOxxOm Oct 27 '15 at 21:09
  • index.html http://pastebin.com/0VtT5Zvc and javascript: http://pastebin.com/5C8nkg1Q – Hawkins Oct 27 '15 at 21:19
  • Tried just the html without any 3rd-party js (as I don't have your entire extension), added `autofocus` to the input field and it worked right away. So the problem is caused by some of those scripts. – wOxxOm Oct 27 '15 at 21:47
  • Go to popup inspector (rightclick the toolbar icon and `Inspect popup`), open `Sources` panel, expand `Event listener breakpoints`, then `Control`, enable `blur` and `focus` breakpoints, then press F5 to reload the popup – wOxxOm Oct 27 '15 at 21:52
  • I've messed about with that just now, and I couldn't make it break. The breakpoints never occurred, so I'm assuming blur or focus never occurred? – Hawkins Oct 27 '15 at 23:41
  • I set a breakpointfor DOMContentLoaded and stepped through each line; when $('#slot2input').focus(); was called and executed, it didn't seem to move the cursor or select the element. Not sure what's happening now... – Hawkins Oct 27 '15 at 23:45
  • Implement the usual technique, divide and conquer. For example remove all other js scripts from the page, ensure the element is autofocused, re-enable the scripts one by one or bisect the whole bunch until you find what was causing the problem. – wOxxOm Oct 30 '15 at 19:52
  • Possible duplicate of [How to steal focus from the omnibox in a Chrome extension on the new tab page?](https://stackoverflow.com/questions/17598778/how-to-steal-focus-from-the-omnibox-in-a-chrome-extension-on-the-new-tab-page) – Hawkins Feb 15 '18 at 00:06

0 Answers0