【问题标题】:Mozilla Firefox Add-on Android - Page Action not workingMozilla Firefox 附加组件 Android - 页面操作不起作用
【发布时间】:2019-01-19 07:54:33
【问题描述】:

我已经在这个问题上工作了几个小时,但似乎找不到任何关于如何在 android 上实现页面操作的好资源。同一个,Differences_between_desktop_and_Android

我将我的应用程序连接到 Firefox 中的 Web 调试器没有任何错误。我试图手动调用一些函数,但没有定义。我可能做错了,但它适用于我的 PC 版 Firefox。

我在 BG 脚本的顶部创建了一些上下文菜单项。我不确定这是否会影响脚本在 android 上的执行。

ReferenceError: browser is not defined[Learn More] debugger eval code:1:1 

下面是创建页面操作的代码,它包含在我的Background.js.

/* *********** */
/* Page Action */
/* *********** */
const TITLE_APPLY = "Stack Open";
const TITLE_REMOVE = "Stack Closed";
const APPLICABLE_PROTOCOLS = ["http:", "https:"];

/*
Based on the current title, Update the page action's title and icon to reflect its state.
*/
function toggleT(tab) {

  function gotTitle(title) {
    if (title === TITLE_APPLY) {
      console.log(tab.id);
      //browser.pageAction.setIcon({tabId: tab.id, path: "pressed.svg"});
      browser.pageAction.setTitle({tabId: tab.id, title: TITLE_REMOVE});
    } else {
      //browser.pageAction.setIcon({tabId: tab.id, path: "nPressed.svg"});
      browser.pageAction.setTitle({tabId: tab.id, title: TITLE_APPLY});
    }
  }

  var gettingTitle = browser.pageAction.getTitle({tabId: tab.id});
  gettingTitle.then(gotTitle);
}

/*
Returns true only if the URL's protocol is in APPLICABLE_PROTOCOLS.
*/
function protocolIsApplicable(url) {
  var anchor =  document.createElement('a');
  anchor.href = url;
  return APPLICABLE_PROTOCOLS.includes(anchor.protocol);
}

/*
Initialize the page action: set icon and title, then show.
Only operates on tabs whose URL's protocol is applicable.
*/
function initializePageAction(tab) {
  if (protocolIsApplicable(tab.url)) {
    browser.pageAction.setIcon({tabId: tab.id, path: "../books.png"});
    browser.pageAction.setTitle({tabId: tab.id, title: TITLE_APPLY});
    browser.pageAction.show(tab.id);
  }
}

/*
When first loaded, initialize the page action for all tabs.
*/
var gettingAllTabs = browser.tabs.query({currentWindow: true, active: true});
gettingAllTabs.then((tabs) => {
  for (let tab of tabs) {
    initializePageAction(tab);
  }
});

/*
Each time a tab is updated, reset the page action for that tab.
*/
browser.tabs.onUpdated.addListener((id, changeInfo, tab) => {
  initializePageAction(tab);
});

/*
Toggle title when the page action is clicked.
*/
browser.pageAction.onClicked.addListener(toggleT);

【问题讨论】:

    标签: javascript android firefox mozilla add-on


    【解决方案1】:

    我再次阅读了文档,以及其他几页。浏览器操作和页面操作在新选项卡中打开 default.html,因此无需在 Android 上使用页面操作。此外,我的 BG 脚本顶部的上下文菜单项导致 BG 脚本无法在 Android 上运行。

    -- 问题解决了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多