Note that there are some explanatory texts on larger screens.

plurals
  1. POChrome Extension - Get DOM content
    primarykey
    data
    text
    <p>I'm trying to access the activeTab DOM content from my popup. Here is my manifest:</p> <pre><code>{ "manifest_version": 2, "name": "Test", "description": "Test script", "version": "0.1", "permissions": [ "activeTab", "https://api.domain.com/" ], "background": { "scripts": ["background.js"], "persistent": false }, "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'", "browser_action": { "default_icon": "icon.png", "default_title": "Chrome Extension test", "default_popup": "index.html" } } </code></pre> <p>I'm really confused whether background scripts (event pages with persistence: false) or content_scripts are the way to go. I've read all the documentation and other SO posts and it still makes no sense to me.</p> <p>Can someone explain why I might use one over the other.</p> <p>Here is the background.js that I've been trying:</p> <pre><code>chrome.extension.onMessage.addListener( function(request, sender, sendResponse) { // LOG THE CONTENTS HERE console.log(request.content); } ); </code></pre> <p>And I'm just executing this from the popup console:</p> <pre><code>chrome.tabs.getSelected(null, function(tab) { chrome.tabs.sendMessage(tab.id, { }, function(response) { console.log(response); }); }); </code></pre> <p>I'm getting:</p> <pre><code>Port: Could not establish connection. Receiving end does not exist. </code></pre> <p><strong>UPDATE:</strong></p> <pre><code>{ "manifest_version": 2, "name": "test", "description": "test", "version": "0.1", "permissions": [ "tabs", "activeTab", "https://api.domain.com/" ], "content_scripts": [ { "matches": ["&lt;all_urls&gt;"], "js": ["content.js"] } ], "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'", "browser_action": { "default_icon": "icon.png", "default_title": "Test", "default_popup": "index.html" } } </code></pre> <p>content.js</p> <pre><code>chrome.extension.onMessage.addListener( function(request, sender, sendResponse) { if (request.text &amp;&amp; (request.text == "getDOM")) { sendResponse({ dom: document.body.innertHTML }); } } ); </code></pre> <p>popup.html</p> <pre><code>chrome.tabs.getSelected(null, function(tab) { chrome.tabs.sendMessage(tab.id, { action: "getDOM" }, function(response) { console.log(response); }); }); </code></pre> <p>When I run it, I still get the same error:</p> <pre><code>undefined Port: Could not establish connection. Receiving end does not exist. lastError:30 undefined </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.
 

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