chrome.history.deleteUrl not working - google-chrome

I just tried chrome.history.deleteURL in an HTML page and it's not working. Can anyone say where I am going wrong?
Urls.html:
<!DOCTYPE HTML>
<html>
<head>
<title>Your History</title>
<style>
body {min-width: 300px;}
</style>
<script type="text/Javascript">
function deleteURL(form){
var urlName = form.url.value;
chrome.history.deleteUrl(urlName);
}
</script>
</head>
<body>
<form onSubmit="deleteURL(this);">
Enter url here : <input type="text" name="url" />
<input type="submit" value="submit" />
</form>
</body>
</html>
manifest.json:
{
"name": "Browser History",
"version": "1.0",
"description": "Shows up the history",
"permissions": [
"history",
"tabs"
],
"browser_action": {
"default_popup": "Urls.html",
"default_icon": "history.jpg"
}
}
After executing the program I can still see the URL which I wanted to delete.

Although I have never used the chrome.history.* API before your code looks like it should work.
Have you remembered to add the required permission to your manifest?
Edit:
Doh! I just realised that your API call is invalid. Try using this updated version of the deleteUrl function;
function deleteURL(form){
chrome.history.deleteUrl({
url: form.url.value
});
}
Notice that I've wrapped the argument in an object with a url property as per the API. Don't know why I didn't see that earlier.

Related

Accessing chrome.* APIs from local HTML

I am developing a chrome extension. This is how my extension is structured:
I have a browser action defined such that clicking on it loads a local HTML file default.html. This is my manifest file:
{
"name": "Example Extension",
"version": "0.1",
"manifest_version": 2,
"browser_action": {
"default_title": "Does nothing.",
"default_icon": "icon.png"
},
"background": {
"scripts": ["extension.js"]
},
"permissions": ["tabs"]
}
This is my extension.js:
chrome.browserAction.onClicked.addListener(function(tab) {
var newURL = chrome.extension.getURL('default.html');
chrome.tabs.create({ url: newURL });
});
This is my default.html:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" href="jquery-1.10.2.min.js"></script>
<script type="text/javascript" href="donothing.js"></script>
</head>
<body>
<b>It works!</b>
</body>
</html>
I want to access chrome's currently open tabs in donothing.js and display some statistics in the default.html. I learn that I can not access chrome.* APIs within my donothing.js. If I have understood the docs correctly, I should declare and define a content script and pass messages as donothing.js --> contentscript.js --> extension.js and vice versa to achieve what I want to. Is there a simpler way to do this? Can someone explain the need for the content script here?
Thanks,

Chrome extension copy and paste permission

I'm writing a google chrome extension . it has two textareas and a button . once the button is clicked , it takes the code from the first textarea , does some logic on it and then pasts and shows the output in the second textarea . it works fine when I open the .html file in the browser but when I wanna use it as an extension it doesn't function and when I click the button nothing happens . I think it's got something to do with permissions , I've already put : "contextMenus",
"clipboardRead",
"clipboardWrite" permissions on my manifest file but it still doesn't work .
I'll appreciate if someone can help me with this .
thanks
edit : codesmanifest.json :
{
"name": "MissLang Fix extension",
"manifest_version": 2,
"version": "1.0",
"description": "fixing english to farsi typings.",
"icons":{
"128":"icon_128.png"
},
"browser_action": {
"default_icon": "icon.png",
"default_popup": "missLangFix.html"
},
"permissions": [
"contextMenus",
"clipboardRead",
"clipboardWrite"
]
}
missLangFix.html :
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
<script src="script.js"></script>
<title>MissLang Fix</title>
</head>
<body>
<center>
<div id="test"></div>
<textarea class="txtarea" id="input" rows="4" cols="50"></textarea>
<!--<div class="myBtn" onClick="myClick()">
<b style="line-height:30px">fix</b>
</div>-->
<br/>
<input type="button" value="fix" onclick="myClick()" />
<br/>
<textarea class="txtarea2" id="output" rows="4" cols="50" readonly="readonly"></textarea>
</center>
</body>
</html>
script.js :
window.onload = myOnload();
function myOnload(){
var myString = document.getElementById("txtarea").innerHTML = "";
}
function myClick(){
var myString = document.getElementById("input").value;
for(var i=0;i<myString.length;i++){
myString = myString.replace("q","ض");
myString = myString.replace("w","ص");
myString = myString.replace("e","ث");
}
//document.getElementById("txtarea").value = myString;
document.getElementById("output").innerHTML = myString;
}
Alright, the problem is the inline onClick handler on your button. Just move that to your script.js onLoad handler and it should start to work. First add an idto the button to make this easy.
<input id="button" type="button" value="fix" />
Next, get that button and add the onclick
function myOnload(){
var myString = document.getElementById("txtarea").innerHTML = "";
var button = document.getElementById("button");
button.onclick= function(){myClick()};
}
...
Here is a fiddle for it: http://jsfiddle.net/x2w2p/1/

chrome.experimental.discovery.suggest not suggesting to chrome://newtab

I have tried to suggest http://www.w3schools.com to chrome://newtab, neither i have success nor errors through discovery.
After a cursory look at source code i have tried the same code in different profiles, but no worth.
registry->Add(extension_id(), suggested_link.Pass());
is not adding my link to chrome://newtab.
Trivial Demonstration
manifest.json
{
"name":"Discovery Demo",
"description":"This demonstrates Discovery API",
"manifest_version":2,
"version":"1",
"permissions":["experimental"],
"browser_action":{
"default_icon":"screen.png",
"default_popup":"popup.html"
}
}
popup.html
<html>
<head>
<script src="popup.js"></script>
</head>
<body>
</body>
</html>
popup.js
chrome.experimental.discovery.suggest({
"urlImage": chrome.extension.getURL("screen.png"),
"score": 1.0,
"linkUrl": "http://www.w3schools.com",
"linkText": "Sample"
});
Is there any thing i missed out, any suggestions?

Chrome extension - start point

Where I can find an example code of chrome extension which shows the current address in a popup?
Thanks.
Ron
Documentation: http://code.google.com/chrome/extensions/getstarted.html
Samples: http://code.google.com/chrome/extensions/samples.html
This is the official documentation and sample code for Google Chrome extensions. In your manifest you want to declare a popup for either a page or browser action (whichever best suites your extension). In your popup HTML file you probably want something like the following;
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function initPopup() {
chrome.tabs.getSelected(null, function (tab) {
document.body.appendChild(document.createTextNode(tab.url));
});
}
</script>
</head>
<body onload="initPopup();"></body>
</html>
This very simply appends the URL of the selected tab to the body of the popup.
Your manifest should look like the following;
{
"name": "My First Extension",
"version": "1.0",
"description": "The first extension that I made.",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"tabs"
]
}
The file structure for this example is a single folder containing manifest.json, popup.html and icon.png.
On the Extensions page (chrome://extensions) you should click Load unpacked extension... and navigate to this folder. If you make any changes to the manifest be sure to click the Reload link to pick up these changes.
I hope this helps but I strongly suggest reading through the documentation I mentioned above to get a better understanding of what you're doing.
EDIT: Added missing null argument to code and included an example manifest and file structure based on additional information gathered from comments.
Try this code in your popup, it works for me (Google Chrome 14-beta):
chrome.windows.getCurrent(function(window) {
chrome.tabs.getSelected(window.id, function(tab) {
console.log(tab);
console.log(tab.url); // url of the current tab
});
});
For more information check: http://code.google.com/chrome/extensions/tabs.html#method-getSelected
It looks like all these answers are outdated so here is a manifest 2 example. You will need jQuery for this example. I have included all the files in a gist.
manifest.json
{
"manifest_version": 2,
"name": "Hello World",
"version": "1.0",
"author": "Christian Juth",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"activeTab"
]
}
popup.html
<!doctype html>
<html>
<head>
<title>Hello World</title>
<script src="jquery.js"></script>
<script src="popup.js"></script>
</head>
<body>
<span id="address"></span>
</body>
</html>
popup.js
$(document).ready(function(){
//define query
var query = { active: true, currentWindow: true };
//query tabs
chrome.tabs.query(query, function (tabs) {
currentAddress = tabs[0].url;
$('#address').text(currentAddress);
});
});

Debugging popup extensions

When I open:
Incpect Popup
Next select tab scripts
I get samtimes empty list scriprs.
I write a simply extension manifest.json (my minimum)
{
"name": "TEST",
"version": "1.0",
"browser_action": {
"popup": "popup.html"
}
}
Popup.html
<!DOCTYPE html>
<html>
<head>
<script src="popup.js"></script>
</head>
<body>
<h1>TEST1</h1>
<div id="content">
TEST
</div>
</body>
</html>
popup.js
console.log('test OK');
Sometimes I get list but not with all my js files.
This file is only short example. I send to console message, and I see this message and I see this file in resource, but i not see in the scripts tab.
My chrome version is 9.0.597.98.
I try with other version and I have the same problem.
During the next update, this will be solved. If it doesn't, please submit a bug in http://crbug.com/new