Расширение Chrome - Получить контент DOM

Я пытаюсь получить доступ к содержимому activeTab DOM из моего всплывающего окна. Вот мой манифест:

{
  "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"
  }
}

Я действительно запутался, стоит ли использовать фоновые сценарии (страницы событий с постоянством: false) или content_scripts. Я прочитал всю документацию и другие SO сообщения, и это все еще не имеет смысла для меня.

Может кто-нибудь объяснить, почему я могу использовать один над другим.

Вот background.js, который я пробовал:

chrome.extension.onMessage.addListener(
  function(request, sender, sendResponse) {
    // LOG THE CONTENTS HERE
    console.log(request.content);
  }
);

И я просто выполняю это из всплывающей консоли:

chrome.tabs.getSelected(null, function(tab) {
  chrome.tabs.sendMessage(tab.id, { }, function(response) {
    console.log(response);
  });
});

Я собираюсь:

Port: Could not establish connection. Receiving end does not exist. 

ОБНОВИТЬ:

{
  "manifest_version": 2,

  "name": "test",
  "description": "test",
  "version": "0.1",

  "permissions": [
    "tabs",
    "activeTab",
    "https://api.domain.com/"
  ],

  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "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"
  }
}

content.js

chrome.extension.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.text && (request.text == "getDOM")) {
      sendResponse({ dom: document.body.innertHTML });
    }
  }
);

popup.html

chrome.tabs.getSelected(null, function(tab) {
  chrome.tabs.sendMessage(tab.id, { action: "getDOM" }, function(response) {
    console.log(response);
  });
});

Когда я запускаю его, я все еще получаю ту же ошибку:

undefined
Port: Could not establish connection. Receiving end does not exist. lastError:30
undefined

5 ответов

Решение

Термины "фоновая страница", "всплывающее окно", "скрипт содержимого" все еще сбивают вас с толку; Я настоятельно рекомендую более подробно изучить документацию по расширениям Google Chrome.

Относительно вашего вопроса, если сценарии содержания или фоновые страницы являются подходящим способом:

Содержание скриптов: определенно
Сценарии содержимого являются единственным компонентом расширения, которое имеет доступ к DOM веб-страницы.

Фоновая страница / Всплывающее окно: Возможно (возможно, максимум 1 из двух)
Вам может потребоваться, чтобы скрипт содержимого передавал содержимое DOM либо на фоновую страницу, либо во всплывающее окно для дальнейшей обработки.


Позвольте мне повторить, что я настоятельно рекомендую более тщательно изучить имеющуюся документацию!
Тем не менее, вот пример расширения, которое извлекает содержимое DOM на страницах Stackru и отправляет его на фоновую страницу, которая, в свою очередь, печатает его в консоли:

background.js:

// Regex-pattern to check URLs against. 
// It matches URLs like: http[s]://[...]stackru.com[...]
var urlRegex = /^https?:\/\/(?:[^./?#]+\.)?stackru\.com/;

// A function to use as callback
function doStuffWithDom(domContent) {
    console.log('I received the following DOM content:\n' + domContent);
}

// When the browser-action button is clicked...
chrome.browserAction.onClicked.addListener(function (tab) {
    // ...check the URL of the active tab against our pattern and...
    if (urlRegex.test(tab.url)) {
        // ...if it matches, send a message specifying a callback too
        chrome.tabs.sendMessage(tab.id, {text: 'report_back'}, doStuffWithDom);
    }
});

content.js:

// Listen for messages
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
    // If the received message has the expected format...
    if (msg.text === 'report_back') {
        // Call the specified callback, passing
        // the web-page's DOM content as argument
        sendResponse(document.all[0].outerHTML);
    }
});

manifest.json:

{
  "manifest_version": 2,
  "name": "Test Extension",
  "version": "0.0",
  ...

  "background": {
    "persistent": false,
    "scripts": ["background.js"]
  },
  "content_scripts": [{
    "matches": ["*://*.stackru.com/*"],
    "js": ["content.js"]
  }],
  "browser_action": {
    "default_title": "Test Extension"
  },

  "permissions": ["activeTab"]
}

Вам не нужно использовать передачу сообщений для получения или изменения DOM. я использовал chrome.tabs.executeScriptвместо. В моем примере я использую только разрешение activeTab, поэтому скрипт выполняется только на активной вкладке.

часть файла manifest.json

"browser_action": {
    "default_title": "Test",
    "default_popup": "index.html"
},
"permissions": [
    "activeTab",
    "<all_urls>"
]

index.html

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <button id="test">TEST!</button>
    <script src="test.js"></script>
  </body>
</html>

test.js

document.getElementById("test").addEventListener('click', () => {
    console.log("Popup DOM fully loaded and parsed");

    function modifyDOM() {
        //You can play with your DOM here or check URL against your regex
        console.log('Tab script:');
        console.log(document.body);
        return document.body.innerHTML;
    }

    //We have permission to access the activeTab, so we can call chrome.tabs.executeScript:
    chrome.tabs.executeScript({
        code: '(' + modifyDOM + ')();' //argument here is a string but function.toString() returns function's code
    }, (results) => {
        //Here we have just the innerHTML and not DOM structure
        console.log('Popup script:')
        console.log(results[0]);
    });
});

Для тех, кто попробовал gkalpak ответить, и это не сработало,

Имейте в виду, что Chrome добавит скрипт содержимого на нужную страницу только в том случае, если расширение включено во время запуска Chrome, а также рекомендуется перезапустить браузер после внесения этих изменений.

Мне не удалось выполнить вышеописанное. Ниже представлена ​​самая простая настройка, которая работала у меня с V3.

манифест.json

      {
  "name": "DOM Reader",
  "version": "1.0",
  "manifest_version": 3,
  "description": "Reads the content of a page.",
  "permissions": [
    "scripting",
    "activeTab"
  ],
  "action": {
    "default_popup": "index.html"
  }
}

index.html

      <!DOCTYPE html>
<html>
  <head></head>
  <body>
    <button id="read-content">Read content</button>
    <script src="contentScript.js"></script>
  </body>
</html>

контентScript.js

      document.getElementById('read-content').addEventListener('click', () => {
    chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
        const tab = tabs[0];

        function printTitle() {
            const title = document.title;
            console.log(title);
        };

        chrome.scripting.executeScript({
            target: { tabId: tab.id },
            func: printTitle,
            //        files: ['contentScript.js'],  // To call external file instead
        }).then(() => console.log('Injected a function!'));
    });
});

Вот версия манифеста v3, которая передает содержимое DOM во всплывающий контекст расширения (страницу действий) с помощью обмена сообщениями (https://developer.chrome.com/docs/extensions/mv3/messaging/) .

манифест.json

      {
  "name": "DOM Reader",
  "version": "1.0",
  "manifest_version": 3,
  "description": "Reads the content of a page.",
  "permissions": [
    "scripting",
    "activeTab"
  ],
  "action": {
    "default_popup": "index.html"
  }
}

index.html

      <!DOCTYPE html>
<html>
  <head></head>
  <body>
    <button id="read-content">Read content</button>
    <script src="contentScript.js"></script>
    <div id='result-div' style="width:500px">
        <code id='result'></code>
    </div>
  </body>
</html>

контентScript.js

      document.getElementById('read-content').addEventListener('click', () => {
    chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
        const tab = tabs[0];
        
        function printTitle() {
            //console.log("inside printTitle func");
            const title = document.title;
            var resultStr = "doc title: " + document.title;
            console.log(resultStr);
            
            // https://developer.chrome.com/docs/extensions/mv3/messaging/
            (async () => {
                const response = await chrome.runtime.sendMessage({info: resultStr});
                // do something with response here, not outside the function
                console.log(response);
            })();
            
            //return resultStr;
        };

        chrome.scripting.executeScript({
            target: { tabId: tab.id },
            func: printTitle,
            //        files: ['contentScript.js'],  // To call external file instead
        }).then(() => console.log('Injected a function!'));
    });
});

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script: " + sender.tab.url :
                "from the extension");
    var resp = request.info;
    if (resp) {
        document.getElementById("result").innerText = resp;
        sendResponse({farewell: "thanks for sending! goodbye"});
    }
  }
);

chrome.runtime.sendMessage()используется в рамкахexecuteScriptфункция для передачи содержимого во всплывающее окно действия.

Другие вопросы по тегам