javascript - Mozilla Firefox 附加组件 Android - 页面操作不起作用
问题描述
我已经在这个问题上工作了几个小时,似乎找不到任何关于如何在 android 上实现页面操作的好的资源。同一个,Differences_between_desktop_and_Android。
我将我的应用程序连接到 Firefox 中的网络调试器没有错误。我试图手动调用一些函数,但没有定义。我可能做错了,但它适用于我的 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);
解决方案
我再次阅读了文档,以及其他几页。浏览器操作和页面操作在新选项卡中打开 default.html,因此无需在 Android 上使用页面操作。此外,我的 BG 脚本顶部的上下文菜单项导致 BG 脚本无法在 Android 上运行。
- 问题解决了。
推荐阅读
- node.js - AWS:reactjs 应用程序的连接被拒绝错误
- c - Sqlite 合并 C 文件无法在 Raspbian 上编译
- swift - SwiftUI onDrag。如何提供多个 NSItemProviders?
- algorithm - 给定元素数组,计算长度为“k”的子序列的数量,这些子序列形成具有给定比率“r”的几何级数
- javascript - vega-lite:可调节的选择?
- css - 水平居中对齐 div 与灵活
- python-2.7 - 为什么 pcapy 不能在我的 Ubuntu 18.04 服务器上安装最新版本?
- python - 如何比较两个字典并更新
- firebase - 谷歌分析和 Firebase 分析之间的区别
- html - 如何使用断点创建自动缩放网格布局?(图片和内容相邻的卡片)