Note that there are some explanatory texts on larger screens.

plurals
  1. POgoogle chrome application code injection not working on certain elements
    primarykey
    data
    text
    <p>I've been messing around with packaged chrome applications today to see how much of a website one can manipulate with the code-injection feature.</p> <p>I've been using twitter.com/#!/login as my sandbox because it contains a pretty diverse array of elements.</p> <p>For some reason, the following code does nothing:</p> <pre><code> chrome.tabs.create({url: 'https://twitter.com/#!/login/'}, function (tab) { chrome.tabs.executeScript(tab.id, {code: "jQuery('.main').remove();"}, function() { console.log('code executed'); }); }); </code></pre> <p>It isn't like I can't access the page at all, because this does work:</p> <pre><code> chrome.tabs.create({url: 'https://twitter.com/#!/login/'}, function (tab) { chrome.tabs.executeScript(tab.id, {code: "jQuery('#page-container').remove();"}, function() { console.log('code executed'); }); }); </code></pre> <p>I've loaded jQuery properly in the manifest and have given permissions to all sites as well.</p> <p>In fact, it seems that I can access any element on the page outside the scope of #page-container.</p> <p>Using the Chrome dev-tools console to mess with the page, I can easily use jQuery to access every element (although I recognize that this is whatever version the page is natively using).</p> <p>I noticed something strange in the tag for #page-container:</p> <pre><code>&lt;div id="page-container" class&gt;...&lt;/div&gt; </code></pre> <p>There is a class attribute with no equals sign or value... could this be blocking me?</p> <p>Any thoughts on why I can't get any further into the page with the injected code?</p> <p><strong>EDIT:</strong> Ok, I found a workaround. Storing the value of #page-container and using setInterval to check for a change takes care of the dynamic problem. </p> <p>Just to be clear, there isn't an event handler that can watch for a change in the value of a div, is there? I just feel like using intervals to check for stuff is so old-fashioned these days, even though it is sometimes necessary...</p> <p>Here's the workaround code:</p> <pre><code> chrome.tabs.create({url: 'https://twitter.com/#!/login/'}, function (tab) { chrome.tabs.executeScript(tab.id, {code: "var initial = jQuery('#page-container').html();var interval = setInterval(function(){var final = jQuery('#page-container').html();if(initial !== final){clearInterval(interval);jQuery('.main').remove();}},10);"}, function() { console.log('code executed'); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload