0

I have a .js file that largely works. Except that a line with a selector and its subsequent console log returns nothing. I tried the code in the console and it works: console log returns what I was expecting. Then the question is, why doesn't it work in the script file? To zone in on the issue, I've commented out every other line in the script. It still doesn't work. What could be wrong in these mere 4 lines? -- they are now all there is in the script file.

 $( document ).ready(function() {
    alert("alert");
    var fullname2 = $('#topcard').find('.profile-info').find('h1').text().replace(/ /g, '_');

   console.log(fullname2);
});

The alert fires, both if I run the code in console, or if I run the .js script.

The above is code in the content.js that is part of a Chrome extension. So I confirmed that the bug is something related to running the .js file before the document is ready, by the following: if I use setTimeout, the bug seems to disappear from running the js. script:

setTimeout(function(){
console.log("hello");

$(document).ready(function() {

    var fullname2 = $('#topcard').find('.profile-info').find('h1').text();
 console.log(fullname2);
});

}, 2000);

By running the script, console logs the fullname correctly.

I found out that setting "run_at" in the manifest.json may make a difference. But I so far haven't made it work. Since the default was "document_idle" and it didn't work when I didn't specify run_at, now I set it as "run_at": "document_end". But it still doesn't work without the setTimeout.

MichM
  • 886
  • 1
  • 12
  • 28
  • what do you mean with: __why doesn't it work in the script file?__ how are you trying to execute the script file? – DIEGO CARRASCAL May 18 '16 at 19:25
  • 3
    If the code works when it isn't in a script file then the problem is either (a) The code around it or (b) Something about the script element used to load it. You've provided neither. – Quentin May 18 '16 at 19:25
  • Let's see how you are including the script file – Charlie Martin May 18 '16 at 19:27
  • maybe one of these selectors don't match any element at the ready event... it could be a pending ajax request that need to finish or a timer the was set etc, so when you run the console these pending things has already done? –  May 18 '16 at 19:28
  • Try clearing your browser cache, to make sure you don't have an old version of the included file saved. – Caleb O'Leary May 18 '16 at 19:33
  • I'm suspecting that the element I'm selecting doesn't finish loading when I use the script file to select... but when I run the code in the console it would have finished loading. Hence I get console log returning what I expected, if I run the code in the console; returning nothing, if I just use the script file. Some of you suspect my script file is entirely not working -- this isn't true. I can put some other alert in the script file, outside of this document.ready block, and it works. Any idea how to do selection of an element only after EVERYTHING has loaded and are ready? – MichM May 18 '16 at 19:43
  • @Quentin Your two points: 1. there is no code around it -- I've commented all the other code out. 2. The script element used to load it -- it's not the issue here, because the rest of the script, say an alert, fires. If you must know, which I think is a red herring, is that the .js file is run as part of a chrome extension. – MichM May 18 '16 at 19:46
  • Try to exclude part by part from `var fullname2 = $('#topcard').find('.profile-info').find('h1').text().replace(/ /g, '_');`. That is `var fullname2 = $('#topcard').find('.profile-info').find('h1').text();` then `var fullname2 = $('#topcard').find('.profile-info').find('h1');` and so on. – Alex Kudryashev May 18 '16 at 20:12
  • @AlexKudryashev I tried part by part. Any part by part works in the console, so it's not the individual part that matters. Part by part breaks down at the second link in the script, but that I think means the second link onwards was not loaded when the selector fired. – MichM May 18 '16 at 20:32
  • I mean in .js file. Also try `/\s/` instead of `/ /`. – Alex Kudryashev May 18 '16 at 20:36
  • @AlexKudryashev this is red herrring. The code works in the console. – MichM May 18 '16 at 20:38
  • If it doesn't work in the file then you have to experiment with file. 1. How it is loaded 2. how your ` – Alex Kudryashev May 18 '16 at 20:41
  • 1
    Please don't post a same question twice. http://stackoverflow.com/questions/37310596/chrome-extension-the-js-script-fires-too-early-despite-run-at-document-idle – Haibara Ai May 18 '16 at 23:51

0 Answers0