Last active
February 18, 2017 11:11
-
-
Save lydell/a730c852fdb37f6d8f1f30968f3b591b to your computer and use it in GitHub Desktop.
WebExtension .focus() tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
function onError(error) { | |
console.error(`Error: ${error}`); | |
} | |
function sendMessageToTabs(tabs) { | |
for (let tab of tabs) { | |
browser.tabs.sendMessage( | |
tab.id, | |
{greeting: "Hi from background script"} | |
).then(response => { | |
console.log("Message from the content script:"); | |
console.log(response.response); | |
}).catch(onError); | |
} | |
} | |
browser.commands.onCommand.addListener(name => { | |
console.log("command", name); | |
browser.tabs.query({ | |
currentWindow: true, | |
active: true | |
}).then(sendMessageToTabs).catch(onError); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
browser.runtime.onMessage.addListener(request => { | |
console.log("Message from the background script:"); | |
console.log(request.greeting); | |
document.body.style.border = "2px solid red"; | |
document.body.focus(); | |
var firstInput = document.querySelector("input:not([type=hidden])"); | |
if (firstInput) { | |
firstInput.style.border = "2px solid red"; | |
firstInput.focus(); | |
} | |
return Promise.resolve({response: "Hi from content script"}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"manifest_version": 2, | |
"name": "webextension-test", | |
"version": "0.0.0", | |
"background": { | |
"scripts": ["background.js"] | |
}, | |
"content_scripts": [ | |
{ | |
"matches": ["<all_urls>"], | |
"js": ["content.js"] | |
} | |
], | |
"browser_action": { | |
"default_popup": "popup.html" | |
}, | |
"commands": { | |
"_execute_browser_action": { | |
"suggested_key": { | |
"default": "Ctrl+E" | |
} | |
}, | |
"test": { | |
"suggested_key": { | |
"default": "Ctrl+Shift+E" | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p id="text">popup</p> | |
<input id="input" autofocus> | |
<script src="popup.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
setTimeout(() => { | |
document.querySelector("body").focus(); | |
text.textContent = "focused?"; | |
}, 100); | |
document.addEventListener("keydown", event => { | |
text.textContent = event.key; | |
}, true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment