Reputation:
Okay, so I've worked through trying to get this to work and I just have no clue; here is my manifest file:
EDIT: Now updated:
{
"manifest_version": 2,
"name": "To Top Button",
"version": "0.1",
"description": "Adds '#top' to the URL.",
"permissions": [
"tabs", "https://forums.robertsspaceindustries.com/*"
],
"background": {
"scripts": ["adder.js"],
"persistent": false
},
"browser_action": {
"default_icon": "icon.png",
"default_title": "To the top!"
}
}
EDIT: changed to a js file:
function updateUrl(tab){
var currentURL = tab.url
var newurl = currentURL.replace(currentURL + "#top");
chrome.tabs.update(tab.id, {url: newurl});
}
chrome.browserAction.onClicked.addListener(function(tab) {updateUrl(tab);});
Now, what I want to do, is when I click the icon, if I'm on a page that is sub to this (https://forums.robertsspaceindustries.com/) it adds "#top" to the end of the URL and then refreshes the page and takes me to the top.
What have I messed up?
Upvotes: 1
Views: 103
Reputation: 21599
I think you have to move your script from HTML file to JS (according to Manifest V2 Security Changes: Inline Scripts) and register it in the manifest.
If your JS is named handler.js
, the manifest should be something like this:
{
"manifest_version": 2,
"name": "To Top Button",
"version": "0.1",
"description": "Adds '#top' to the URL.",
"permissions": [
"tabs", "https://forums.robertsspaceindustries.com/*"
],
"background": {
"scripts": ["handler.js"],
"persistent": false
},
"browser_action": {
"default_icon": "icon.png",
"default_title": "To the top!"
}
}
As for the code, try
function updateUrl(tab){
var newurl = tab.url.replace(/(#[^#]*)?$/, "#top");
chrome.tabs.update(tab.id, {url: newurl});
}
chrome.browserAction.onClicked.addListener(function(tab) { updateUrl(tab); });
Upvotes: 1