相关疑难解决方法(0)

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"
  }
}
Run Code Online (Sandbox Code Playgroud)

我真的很困惑后台脚本(具有持久性的事件页面:false)或content_scripts是否可行.我已经阅读了所有文档和其他SO帖子,但对我来说仍然没有意义.

有人可以解释为什么我可以使用一个而不是另一个.

这是我一直在尝试的background.js:

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

我只是从弹出控制台执行此操作:

chrome.tabs.getSelected(null, function(tab) {
  chrome.tabs.sendMessage(tab.id, { }, function(response) {
    console.log(response);
  });
});
Run Code Online (Sandbox Code Playgroud)

我越来越:

Port: Could not establish connection. Receiving end does not …
Run Code Online (Sandbox Code Playgroud)

javascript google-chrome-extension

97
推荐指数
3
解决办法
13万
查看次数

使用executeScript()将弹出窗口中弹出的数据与popup注入的内容脚本进行通信

我的Chrome扩展程序弹出窗口中有一个文本区域和一个按钮.我希望用户在文本区域中输入所需的文本.然后,一旦他们点击该按钮,它将注入一个内容脚本,以更改当前页面上的字段<textarea class="comments">文本,该文本必须是用户<textarea>在Chrome扩展程序弹出窗口中输入的文本.

我的问题是,我怎样才能从文本<textarea>中我popup.html,并从它传递popup.js的内容脚本?

这就是我目前所拥有的:

popup.html:

<!doctype html>  
<html>  
    <head><title>activity</title></head>  
<body>  
    <button id="clickactivity3">N/A</button> 
    <textarea rows="4" cols="10" id="comments" placeholder="N/A Reason..."></textarea>
    <script src="popup.js"></script> 
</body>
</html>  
Run Code Online (Sandbox Code Playgroud)

popup.js:

function injectTheScript3() {
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        // query the active tab, which will be only one tab
        //and inject the script in it
        chrome.tabs.executeScript(tabs[0].id, {file: "content_script3.js"});
    });
}

document.getElementById('clickactivity3').addEventListener('click', injectTheScript3);
Run Code Online (Sandbox Code Playgroud)

content_script3:

//returns a node list which is good
var objSelectComments = …
Run Code Online (Sandbox Code Playgroud)

html javascript google-chrome-extension firefox-addon-webextensions

7
推荐指数
1
解决办法
3256
查看次数

如何将参数从后台脚本传递到 Chrome 扩展中的内容脚本?

我有一个 Chrome 扩展,它从后台脚本调用内容脚本以将 HTML 插入网页。

当我调用内容脚本 (inject.js) 时,我想从内容脚本 (eventpage.js) 传递一些参数,但这样做时遇到一些问题。我也不想使用利用 chrome.storage 或 localstorage 的解决方案。

Manifest.json(相关部分):

{
  "manifest_version": 2,
  "content_scripts": [
    {
      "matches": ["http://*/*"],
      "js": ["inject.js"]
    }
  ],
  ...     
  "background": {
    "scripts": ["eventpage.js",...],
    "persistent": false
  },

}
Run Code Online (Sandbox Code Playgroud)

Eventpage.js(背景):

// Want to add the parameter here

chrome.tabs.executeScript(tabId, {
    file: 'inject.js'
});
Run Code Online (Sandbox Code Playgroud)

Inject.js(内容):

(function() {

    // Want to retrieve the parameter passed from eventpage.js here

})();
Run Code Online (Sandbox Code Playgroud)

javascript google-chrome-extension

5
推荐指数
1
解决办法
3893
查看次数

browser.tabs.sendMessage(): 错误:接收端不存在

我试图运行tabs.sendMessage()MDN 页面上给出的示例代码。所以我的代码是:

清单.json

{
    "manifest_version": 2,
    "name": "test1",
    "version": "1.0",
    "description": "test",
    "icons": {
        "48": "icons/Ruler48.png"
    },
    "permissions": [
        "notifications",
        "tabs",
        "activeTab"
    ],
    "browser_action": {
        "default_icon": "icons/Ruler48.png",
        "default_title": "test"
    },
    "content_scripts": [{
        "matches": ["*://*/"],
        "js": ["content-script.js"]
    }],
    "background": {
        "scripts": ["bgS.js"]
    }
}
Run Code Online (Sandbox Code Playgroud)

bgS.js:

function onError(error) {
  console.error(`Error: ${error}`);
}

function sendMessageToTabs(tabs) {
  for (let tab of tabs) {
    console.log(tab.id);
    browser.tabs.sendMessage(
      tab.id,
      {greeting: "Hi from background script"}
    ).then(response => {
      console.log("Message from the content script:");
      console.log(response.response); …
Run Code Online (Sandbox Code Playgroud)

javascript firefox-addon-webextensions

2
推荐指数
1
解决办法
3399
查看次数

将参数传递给 chrome.scripting.executeScripts 执行的文件脚本?(清单 v3)

我正在开发一个 chrome 扩展(manifest v3),并且想将参数传递给我在chrome.scripting.executeScripts. 但是,文档提到仅在指定参数args时才有效func

我发现了Manifest v2 的类似问题chrome.tabs.executeScript,使用它有一个解决方案,但我无法在清单 v3 中使用类似的方法。

脚本与func(工作)

// popup.js
chrome.scripting.executeScript({
    target: { tabId: tabId, allFrames: false },
    args: [eleID, type, offsetHeight + 10],
    func: scrollToTarget
});

function scrollToTarget(eleID, type, headerHeight = 40) {
   console.log({eleID, type, headerHeight);
}
Run Code Online (Sandbox Code Playgroud)

不工作

脚本与files

这是与清单 v2类似的方法chrome.tabs.executeScript

// popup.js
chrome.scripting.executeScript({
    target: { tabId: tabId, allFrames: false },
    code: `var eleID = '${eleID}'; var type = …
Run Code Online (Sandbox Code Playgroud)

javascript firefox-addon google-chrome-extension

1
推荐指数
1
解决办法
1874
查看次数

document.getElementById 返回空值

你好,我有这些 div:

<div id="layoutGroup1">
    <h2>UK Map</h2>
    <div div style="width: 650px; height: 700px;" id="MapDIV"></div>
    <div id="userUpdateDIV"></div>
    <div id="BarChartDIV"></div>
    <div id="divPack1"></div>
</div>
<div id="layoutGroup2">
    <div id="tree"></div>
</div>
<div id="layoutGroup3">
    <div id="map"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

我希望在屏幕上显示三个按钮,然后单击隐藏两个 div 并仅显示一个。

button id="userButton" onclick ="showOnClick('layoutGroup1');">ECA </button
Run Code Online (Sandbox Code Playgroud)
button id="userButton" onclick ="showOnClick('layoutGroup1');">ECA </button
Run Code Online (Sandbox Code Playgroud)

上面是我使用的函数,尽管它给了我一个错误,即getElementByIDis null

html javascript

-3
推荐指数
1
解决办法
3万
查看次数