Chrome Extension to pull text from script

后端 未结 1 1614
情书的邮戳
情书的邮戳 2021-01-07 10:09

I\'m attempting to pull \"webId%22:%22\" var string from a script tag using JS for a Chrome extension. The example I\'m currently working with allows me to pull the page tit

相关标签:
1条回答
  • 2021-01-07 10:20
    1. You have inline code in onclick attribute which won't work, see this answer.
      You can see an error message in devtools console for the popup: right-click it, then "Inspect".
      The simplest solution is to attach a click listener in your popup.js file.

    2. The popup is an extension page and thus it can access chrome.tabs.executeScript directly without chrome.extension.getBackgroundPage(), just don't forget to add the necessary permissions in manifest.json, preferably "activeTab".

    3. For such a simple data extraction you don't even need a separate content script file or messaging, just provide code string in executeScript.

    chrome.tabs.executeScript({
      code: '(' + (() => {
        for (const el of document.querySelectorAll('script[if="inlineJs"]')) {
          const m = el.textContent.match(/"([^"]*%22webId%22[^"]*)"/);
          if (m) return m[1];
        }
      }) + ')()',
    }, results => {
      if (!chrome.runtime.lastError && results) {
        document.querySelector('p').textContent = decodeURI(results[0]);
      }
    });
    
    0 讨论(0)
提交回复
热议问题