相关疑难解决方法(0)

Firefox webextension错误:无法建立连接.接收端不存在

我正在尝试将后台脚本中的变量发送到与HTML页面关联的内容脚本.内容脚本使用从后台脚本接收的变量更新HTML内容.

问题是我收到此错误消息:

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

后台脚本main.js:

var target = "<all_urls>";
function logError(responseDetails) {
  errorTab = responseDetails.tabId;
  console.log("Error tab: "+errorTab);

  errorURL = responseDetails.url;
  console.log("Error URL: "+errorURL);

  //send errorURL variable to content script
  var sending = browser.tabs.sendMessage(errorTab, {url: errorURL})
    .then(response => {
      console.log("Message from the content script:");
      console.log(response.response);
    }).catch(onError);

  //direct to HTML page
  browser.tabs.update(errorTab,{url: "data/error.html"});
}//end function

browser.webRequest.onErrorOccurred.addListener(
  logError,
  {urls: [target],
  types: ["main_frame"]}
);
Run Code Online (Sandbox Code Playgroud)

该error.html方法是:

<html>
<head>
  <meta charset="UTF-8">
</head>
<body> …
Run Code Online (Sandbox Code Playgroud)

javascript firefox firefox-addon firefox-addon-webextensions

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

javascript在window对象上调用带有参数的函数

所以我试图调用这个方法:

refreshMoradaLabel = function(idPlaceHolder) {...};

使用窗口对象:

window [ refreshMoradaLabel('id') ] ();

但这似乎不起作用。只有当方法没有参数时。有没有办法使用window [ variable ] ()语法来工作?

编辑;

好的,这是代码:

moarada.jsp 有这些方法的代码:

<c:set var="methodOnClose" value="refreshDynamicValues" />
<c:if test="${empty fieldInstance || (not empty fieldInstance && isTramitacao)}">
  <c:set var="methodOnClose" value="refreshMoradaLabel(${dfieldId})" />
</c:if>
<a class="texto" href="#" onclick="editMoradaPopup('${dfieldId}','${methodOnClose}');" id="moradas_${dfieldId}"><img alt="${moradaDes}" src="${pageContext.request.contextPath}/images/icon/icon-pesquisa.png"></a>
Run Code Online (Sandbox Code Playgroud)

window.refreshMoradaLabel = 函数(idPlaceHolder){

    alert("label:" +idPlaceHolder);
    if($F(idPlaceHolder) != '') {
        //Update label
        new Ajax.Updater(idPlaceHolder+'_label', 'moradaPopupLocaleAware.do2?method=getLabel', 
                {method: 'get', parameters: 'id='+$F(idPlaceHolder)});
    }

};
Run Code Online (Sandbox Code Playgroud)

window.editMoradaPopup= function(idPlaceHolder,method){ alert(idPlaceHolder); Ext.onReady(function(){ action = "${pageContext.request.contextPath}/moradaPopupLocaleAware.do2"; action += "?method=edit&id="+$(idPlaceHolder).value;

        action += "&idPlaceHolder="+idPlaceHolder; …
Run Code Online (Sandbox Code Playgroud)

javascript window

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

浏览器扩展:在浏览器操作弹出窗口和后台脚本之间发送消息(带响应)

首先,我不是在寻找长期连接。我特别在寻找一种发送消息和直接响应这些消息的方法。

当您在 content-script 和 background-script 之间发送消息时,它非常简单,因为您使用 chrome.tabs API 从/向内容脚本发送/接收消息。并且要从/向后台脚本发送/接收消息,您可以使用 chrome.runtime API。

但是浏览器操作弹出窗口有点不同,因为两者都在后台上下文中运行。所以我想他们都必须使用 chrome.runtime API。

但这意味着我必须chrome.runtime.onMessage在浏览器操作弹出窗口和后台脚本中收听。所以基本上我会在后台脚本中接收从弹出窗口发送的消息,但也会在弹出窗口本身中接收消息。反过来也是一样。

所以是的,这真的行不通:

/////////////background-script//////////////
//Send message from background-script to browser-action-popup
chrome.runtime.sendMessage({msg:"This is a message sent from the background-script to the browser-action-popup"})
  .then(response => { //Receive response from the browser-action-popup
      console.log(response.msg)
  })

//Receive messages from browser-action-popup
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse) {
    sendResponse({msg:"This is a response message sent from the background-script"})
    return true
})
Run Code Online (Sandbox Code Playgroud)

...

///////////browser-action-popup/////////////
//Send message from browser-action-popup to background-script
chrome.runtime.sendMessage({msg:"This is a message sent from the browser-action-popup …
Run Code Online (Sandbox Code Playgroud)

javascript google-chrome-extension firefox-addon-webextensions

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