src: fix commandline_frame/background messaging

The commandline iframe needs to be messaged with the tabs API, as if
it's a content script.
This commit is contained in:
Colin Caine 2017-10-06 04:16:02 +01:00
parent 07c3688f8d
commit df3ca534ed
4 changed files with 43 additions and 19 deletions

View file

@ -16,13 +16,19 @@ let cmdline_iframe: HTMLIFrameElement = undefined
function init(){
cmdline_iframe = window.document.createElement("iframe")
cmdline_iframe.setAttribute("src", browser.extension.getURL("static/commandline.html"))
hide()
window.document.body.appendChild(cmdline_iframe)
}
// TODO: Propagate awaits back through messaging system or resend
// commandline_frame messages from excmd_content if you want to avoid init'ing
// every time.
init()
export function show(){
if (cmdline_iframe === undefined) {
init()
}
// if (cmdline_iframe === undefined) {
// init()
// }
cmdline_iframe.setAttribute("style", "position: fixed; top: 0; left: 0; z-index: 10000; width: 100%; height: 36px; border: 0; padding: 0; margin: 1;");
}

View file

@ -21,8 +21,11 @@ function process() {
}
function changecommand(newcommand?: string){
clInput.value = newcommand
console.log(newcommand, clInput.value)
if (newcommand !== undefined) {
clInput.value = newcommand
}
// Focus is lost for some reason.
clInput.focus()
}
function handler(message) {

View file

@ -9,12 +9,23 @@ interface ContentCommandMessage extends Message {
args?: Array<any>
}
function messageCommandline(command: string, args?: Array<any>) {
browser.runtime.sendMessage({
/** The first active tab in the currentWindow.
*
* TODO: Highlander theory: Can there ever be more than one?
*
*/
async function activeTab() {
return (await browser.tabs.query({active:true, currentWindow:true}))[0]
}
async function messageCommandline(command: string, args?: Array<any>) {
let message: Message = {
type: 'commandline_frame',
command,
args,
})
}
// For commandlines not in iframes on content scripts, use runtime
browser.tabs.sendMessage((await activeTab()).id, message)
}
function messageActiveTab(command: string, args?: Array<any>) {
@ -25,10 +36,13 @@ async function messageFilteredTabs(filter, command: string, args?: Array<any>) {
let message: ContentCommandMessage = {type: "excmd_contentcommand", command: command}
if (!(args == undefined)) message.args = args
let filtTabs = await browser.tabs.query(filter)
filtTabs.map((tab) => {
browser.tabs.sendMessage(tab.id,message)
})
browser.tabs.sendMessage((await activeTab()).id, message)
// Old code for reference in case more than one tab can be active...
// let filtTabs = await browser.tabs.query(filter)
// filtTabs.map((tab) => {
// browser.tabs.sendMessage(tab.id,message)
// })
}
function hasScheme(uri: string) {
@ -54,15 +68,15 @@ export function scrolldownpage(n = 1) { messageActiveTab("scrollpage", [n]) }
export function scrolluppage(n = 1) { scrolldownpage(n*-1) }
export async function scrolldownhalfpage(n = 1) {
const current_window = await browser.windows.getCurrent()
scrolldown(n*0.5*current_window.height)
const current_window = await browser.windows.getCurrent()
scrolldown(n*0.5*current_window.height)
}
export function scrolluphalfpage(n = 1) { scrolldownhalfpage(n*-1) }
export function scrolltobottom() { scrolldown(999999999) } // maximum value scrolldown would respond to
export async function scrolltotop() {
const current_window = await browser.windows.getCurrent()
messageActiveTab("scrollto", [current_window.left, 0])
const current_window = await browser.windows.getCurrent()
messageActiveTab("scrollto", [current_window.left, 0])
}
// Tab functions
@ -101,8 +115,8 @@ export async function reload(n = 1, hard = false){
// Commandline function
export function showcommandline(exstr?){
messageCommandline("changecommand", [exstr,])
messageActiveTab("showcommandline")
messageCommandline("changecommand", [exstr,])
}
export function hidecommandline(){
@ -123,7 +137,7 @@ export async function reloadhard(n = 1){
/** Switch to the next tab by index (position on tab bar), wrapping round.
optional increment is number of tabs forwards to move.
*/
*/
export async function tabnext(increment = 1) {
try {
// Get an array of tabs in the current window

3
src/tridactyl.d.ts vendored
View file

@ -13,7 +13,8 @@ interface Message {
"excmd_contentcommand" |
"keydown" |
"keydown_suppress" |
"commandline"
"commandline" |
"commandline_frame"
// And other unknown attributes...
[key: string]: any
}