Chrome commands API browser back/forward shortcut key? - google-chrome

I'm taking a look Chrome commands API, the document says you can do control these keys for the chrome extension actions:
"_execute_browser_action": {
"suggested_key": {
"windows": "Ctrl+Shift+Y",
"mac": "Command+Shift+Y",
"chromeos": "Ctrl+Shift+U",
"linux": "Ctrl+Shift+J"
}
},
But for what I want to implement, I need to overwrite shortcut key of "browser back/forward" moues key (button). Is that possible to code for that?

Related

Chrome web extension not showing notification even though it is working fine in Firefox

I have a simple call to the chrome.notifications.create(id,options) function and I have checked the arguments ten times already.
The id argument is a string.
The options argument is an object like this:
{type:"basic",message:"message",title:"message",iconUrl:chrome.extension.getURL("icons/icon.png")}
It is only working in Firefox though. Edge Opera and Chrome all fail to display the notification. Edge just crashes!
I've checked everything. There are no errors and the iconUrl is correct.
Also I checked the name field of the manifest json. It's fine.
(Microsoft Edge notification in an extension)
Sample relevant and simplified code
This simplified version has the same problem as the complete one.
//For hooking up event handlers
try {
chrome.runtime.onStartup.addListener(doHandshake);
}catch(ex) {
console.error("onStartup function in Edge is not supported.");
}
chrome.webRequest.onBeforeRequest.addListener(onBeforeRequestCallback, { types:['main_frame'],urls:['*://*/*']}, ['blocking']);
function onBeforeRequestCallback(requestDetails) {
showMessage();
return {};
}
function showMessage() {
console.log("about to show notification...");
var notificationOptions ={type:"basic",message:"msg",title:"title",iconUrl:chrome.extension.getURL("icons/icon.png")};
chrome.notifications.create("",notificationOptions);
}
The manifest.json
{
"manifest_version": 2,
"author" : "whatever Ltd.",
"name": "21charsName",
"version": "1.0",
"description": "whatever",
"permissions": [
"*://*/*",
"tabs",
"webRequest",
"webRequestBlocking",
"storage",
"notifications"
],
"browser_action": {
"default_icon": "icons/icon.png",
"default_title": "extension"
},
"background": {
"scripts": [
"background.js"
],
"persistent": true
},
"web_accessible_resources": []
}
It turned out that it's a bug in chromium-based browsers.
It seems once you disable notifications for an extension it never ever show s notifications again because there's no way to re-enable it, and you'll have to reinstall the browser, though my attempt at that yielded nothing either.
I had to try on another machine with a brand new Chrome installation and the notifications started showing on that machine.
As for Edge, it turned out to be a bug too. :-/
If you are using Mac and Chrome 59+, it might be because the MacOS native notification is disabled for Chrome. Here's two possible solutions:
Solution 1
Open Chrome > Go chrome://flags > Search Enable native notifications > Change it to Disabled > Relaunch Chrome
Solution 2
Go to MacOS System Preferences > Notifications > Turn on Notifications for Banners/Alerts as shown here (likely previously it's Off)
Reference 1
Reference 2

Chrome webNavigation.onComplete not working?

I'm trying to write a chrome extension that executes some code when the user is on a youtube page with a video. As far as I can tell, my code is correct, but It isn't working.
eventPage.js:
chrome.webNavigation.onCompleted.addListener(function(){
console.log("Test")
},{url: [{pathContains: "watch", hostSuffix: "youtube.com"}]});
and my manifest file
{
"manifest_version": 2,
"name": "youtubeExtension",
"description": "A chrome extension for youtube",
"version": "0.1",
"permissions": ["https://www.youtube.com/", "webNavigation"],
"background": {
"scripts": ["eventPage.js"],
"persistant": false
}
}
It seems onCompleted doesn't work on youtube.
Instead of using webNavigation you can also use
Add this to your background.js
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.status == 'complete') {
if (tab.url.indexOf("youtube.com") != -1) {
alert("Youtube load complete");
injectScripts();
}
}
});
and add the following to your mainfest.json
"permissions": ["https://www.youtube.com/", "webNavigation","tabs"]
This will work just as well
chrome.webNavigation.onCompleted is only triggered when a document has finished loading. When you navigate to a different page on YouTube, the new page is not really "loaded" in a traditional way, but the page is dynamically updated, and the URL is replaced using history.pushState. To detect this kind of "navigations", use the chrome.webNavigation.onHistoryStateUpdated event in addition to the onCompleted event.
Another way to detect videos on YouTube pages is by using content scripts on YouTube and some event specific to YouTube, see this answer.

disable refresh / back / forward in OWN browser

Is there a way to only make my OWN browser (Chrome) not be able to go back / forward / refresh?
This happens rather often that when Im developing and playing around in devtools (Changing HTML and CSS just to try things out) I sometimes accidentally swipe back or out of habit hit refresh. I would like to be able to disable the back or forward button via some sort of extension?
I am NOT trying to disable the button on any live-website, just for me locally. Any ideas?
If you want to prevent accidental navigations, there's no need to install any extension. Just open the console, and run the following code:
window.onbeforeunload = function() {
return 'Want to unload?';
};
With this code, you will get a confirmation prompt.
If you really want to prevent the page from unloading via an extension, use the technique described in the answers to How to cancel webRequest silently in chrome extension.
Here's a minimal demo extension that adds a button to your browser. Upon click, you cannot navigate to a different page any more. You can still close the tab without any warning, though:
// background.js
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.webRequest.onBeforeRequest.addListener(function(details) {
var scheme = /^https/.test(details.url) ? 'https' : 'http';
return { redirectUrl: scheme + '://robwu.nl/204' };
// Or (seems to work now, but future support not guaranteed):
// return { redirectUrl: 'javascript:' };
}, {
urls: ['*://*/*'],
types: ['main_frame'],
tabId: tab.id
}, ['blocking']);
});
manifest.json for this extension:
{
"name": "Never unload the current page any more!",
"version": "1",
"manifest_version": 2,
"background": {
"scripts": ["background.js"],
"persistent": true
},
"browser_action": {
"default_title": ""
},
"permissions": [
"<all_urls>",
"webRequest",
"webRequestBlocking"
]
}

Chrome omnibox events without specific keywords?

I do want to check the content written in Chrome omnibox and redirect to the proper page.
Still, I cannot use specific keywords because I do want to redirect things like BUG-1234 to http://bugs.example.com/BUG-1234
I do have a regexp for this (as the BUG part can have lots of values).
How can I do this?
A chrome extension can help you, with help of Omnibox.
If i understood correctly when you enter BUG-1234 and hit Enter in Omnibox, your webpage URL Should be http://bugs.example.com/BUG-1234
Demonstration
I have used keyword as
"keyword": "BUG"
BUG, you can change it as per functionality. So when you enter B+U+G in chrome Omnibox , the search provider adds a custom layer as shown here
Image 1)
and when you enter 1234 and hit Enter or Select the suggested URL Open Bug %s ? in Omnibox, as shown here
Image 2)
It opens a web page with URL as shown here, where i used http://bugs.example.com as a test URL, which can be extended further.
Image 3)
manifest.json
Registered background Page and Omnibox with Chrome Extension, and added related permissions.
{
"name": "Bug Tracker",
"description": "This integrates chrome omnibox with bug search",
"version": "1",
"manifest_version": 2,
"background": {
"scripts": [
"background.js"
]
},
"omnibox": {
"keyword": "BUG"
},
"permissions": [
"<all_urls>"
]
}
background.js
Script for Custom Suggestions
//Set Text to show for custom suggested URL(s)
chrome.omnibox.setDefaultSuggestion({
"description": "Open Bug %s ?"
});
//Fired when Enter or a suggested Link is selected
chrome.omnibox.onInputEntered.addListener(function (bugId) {
//Use your custom URL
chrome.tabs.update({
"url": "http://bugs.example.com/BUG-" + bugId
}, function () {
console.log("Bug Page is open");
});
console.log("Input Entered is " + bugId);
});
References
Background Pages
Omnibox API
Manifest File

Create new tab from browserAction in Chrome [duplicate]

How can I create an extension for Chrome that adds an icon to the toolbar, and when you click it, it opens a new tab with some local web page (for example: f.html)?
I saw this question, but it doesn't really explains what should I add in the manifest file...
This is not true for newer chrome apps.
Newer chrome apps having manifest_version: 2
requires the tabs be opened as:
chrome.browserAction.onClicked.addListener(function(activeTab)
{
var newURL = "http://www.youtube.com/watch?v=oHg5SJYRHA0";
chrome.tabs.create({ url: newURL });
});
Well, in the extensions docs, it states in manifest, you would need to include "tabs" as its permission. Same way they explain the hello world application:
Manifest File:
{
"name": "My Extension",
"version": "1.0",
"description": "Opens up a local webpage",
"icons": { "128": "icon_128.png" },
"background_page": "bg.html",
"browser_action": {
"default_title": "",
"default_icon": "icon_19.png"
},
"permissions": [
"tabs"
],
}
Within the background page, you listen to the mouse click event on the browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.create({'url': chrome.extension.getURL('f.html')}, function(tab) {
// Tab opened.
});
});
As you noticed above, you will see that I used the question you saw in the other post. Note, this isn't tested, but I believe it should work.
chrome.tabs.create need the permission of "tabs".
Simply using window.open in extension without need of any permission. and the code is shorter. I suggest this solution.
window.open(url,'_blank');