【发布时间】: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