Chrome extension gives feedback by comparing URLs - google-chrome-extension

I am making chrome extension. This is a program that checks whether a particular site is right by comparing the URL when you access Google.
The thing I want to do is compare URLs and send an alert when I connect.
However, it does not take effect until you click the chrome extension program. Please let me know the event you need.
main.js
chrome.tabs.executeScript({
code: 'document.URL;'
}, function (domain) {
var google = 'https://www.google.co.kr/';
if(domain == google){
alert('is google')
}
else
alert('not google')
})
manifest.json
{
"manifest_version": 2,
"name": "site detect",
"description": "site",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"tabs"
]
}

Related

Chrome Extension Blocked from pulling data for some users

I've got a simple Chrome Extension that allows users to create and modify a library of comments they can add to text entry forms via the right-click menu.
It's optimized for Google Docs and commenting.
Some users are clearly blocked from pulling data from the database and I don't know why - I can't replicate.
You can use the free version to test - it should create a user on the server, then give you a right-click menu ("Annotate") that has sub-items under Argument.
Users with the problem don't see any sub-items because the values aren't pulled from the db.
The data exchange is very simple: the extension pulls a string from a php script on my server, then splits it. Here's the code that pulls the data...
Any thoughts on why some users are blocked?
chrome.storage.sync.get('userID', function (results){
if (typeof results.userID !== 'undefined') {
userID = results.userID;
var url = "http://www.11trees.com/XXXXXXXX.php?user=" + userID;
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
xmlhttp.onreadystatechange = loadComplete;
} else {
chrome.windows.create({'url': "http://www.11trees.com/XXXXXXX.php"});
}
});
function loadComplete(){
if (xmlhttp.readyState == 4) {
out = xmlhttp.responseText.split("_000_");
}
}
And the Manifest (maybe I need more permissions?):
"externally_connectable": {
"matches": ["*://localhost/*.php/*"]
},
Update: one specific user with this problem tried both school and personal Google accounts, home and school internet, and multiple devices including a ChromeBook. Through screenshots she confirmed that the extension installs just fine, that she doesn't have any unusual permissions set on her Chrome account.
The 00003.log file is just empty - where there should be a small dataset, downloaded through the script, there is just no content in local storage.
Could it be antivirus that is blocking the download?
Adding full extension per Xan's request
{
"manifest_version": 2,
"name": "Annotate for Chrome",
"short_name": "Annotate",
"description": "Right-click access to a pre-written library of comments. Write it once, to perfection, and reuse forever!",
"version": "2.5.0.3",
"permissions": ["identity.email", "https://www.googleapis.com/", "clipboardWrite", "clipboardRead", "activeTab", "tabs", "contextMenus", "storage", "webNavigation", "*://*/*", "http://*/*", "https://*/*"],
"externally_connectable": {
"matches": ["http://*.11trees.com/*.php/*"]},
"oauth2": {
"client_id": "425460661917- smaehlat7c66p1ns6t90ssj3jmlrrecm.apps.googleusercontent.com",
"scopes": [
"https://www.googleapis.com/auth/chromewebstore.readonly"
]
},
"background": {"scripts": ["/scripts/background.js", "/scripts/buy.js", "/scripts/accessComments.js", "/scripts/contextMenus.js"]},
"content_scripts": [
{
"matches": ["http://*/*","https://*/*"],
"js": ["/scripts/content.js"]
}
],
"icons": {
"16": "icon.png",
"48": "icon.png",
"128": "icon.png"
},
"browser_action": {
"default_icon": {
"19": "icon.png",
"38": "icon.png"
},
"default_title": "Annotate for Google Chrome",
"default_popup": "popup.html"
}
}

How to change the color of icon according to user login

I have made an extension to my website.It navigates to my home page which is my login page.When the user log on to my site,the extension icon must appear green and when user log out, the icon should appear red. Please someone help me. I have been trying these two days, but I am not getting how to do it.
Here is my manifest.json file:
{
"name": "My First Extension",
"version": "1.0",
"description": "The first extension that I made.",
"manifest_version": 2,
"browser_action": {
"default_icon": "icon_128.png"
},
"background": {
"scripts": ["background.js"]
}
}
Here is my background.js file:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.create({'url': "http://www.example.com"});
});
Here is my manifest.json:
{
"name": "Calpine Extension",
"version": "1.0",
"description": "Log on to calpinemate",
"manifest_version": 2,
"browser_action": {
"default_icon": "icon_128.png"
},
"background": {
"persistent": false,
"scripts": ["background.js"]
},
"browser_action": {
"default_title": "Test Extension",
"default_icon": "calpine_not_logged_in.png"
},
"externally_connectable": {
"matches": ["http://calpinemate.com"]
}
}
One solution is to communicate the login status from your webpage to your extension (as explained here).
From your webpage you have to send messages to the extension to inform it about the user's login status.
Once the user successfully logs in, make sure you let the extension know:
chrome.runtime.sendMessage(
<your-extension-id>,
{status: 'LOGGED_IN'});
Once you detect that the user's session has ended (either expired or due to manually logging out), make sure you let the extension know:
chrome.runtime.sendMessage(
<your-extension-id>,
{status: 'LOGGED_OUT'});
From your extension listen for messages from the webpage and update accordingly, e.g. change the extension's icon. You'll need two icons (e.g. red.png for indicating logged-out user and green.png for indicating logged-in user) and the source code below:
background.js:
var url = '<your-webpage-url>';
// Listen for external messages (messages from web-pages).
chrome.runtime.onMessageExternal.addListener(function (msg, sender) {
if (sender.url === url) {
// OK, this page is allowed to communicate with me.
if (msg.status === 'LOGGED_IN') {
// Cool, the user is logged in.
chrome.browserAction.setIcon({path: 'green.png'});
} else if (msg.status === 'LOGGED_OUT') {
// How sad, the user is logges out.
chrome.browserAction.setIcon({path: 'red.png'});
}
}
});
manifest.json:
{
"manifest_version": 2,
"name": "Test Extension",
"version": "0.0",
"background": {
"persistent": false,
"scripts": ["background.js"]
},
"browser_action": {
"default_title": "Test Extension",
"default_icon": "red.png"
},
"externally_connectable": {
"matches": ["<your-webpage-url>"]
}
}
U can keep 2 icon 1 in red and other in green and track session if login then show green icon else red

How to develop a simple chrome extension that navigate to a website

I need to create a simple chrome extension which when clicked have to navigate to that website.I have been searching long.But I couldn't find out.Please help me.
Here is my manifest.json file
{
"name": "My First Extension",
"version": "1.0",
"description": "The first extension that I made.",
"manifest_version":2,
"browser_action": {
"default_icon": "icon_128.png"
},
"background_page": "background.html",
"permissions": [
"tabs"
]
}
this is my background.html
<script>
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.create({'url': "http://calpinemate.com"});
</script>
I am just trying to navigate into my site.But it is not working.Anyone please help me.I only have these two files in my directory and an image icon_128.png.Please help me
background_page is no longer supported in manifest_version 2. Instead use the new format. You can also remove the tabs permissions value since chrome.tabs.create doesn't require it.
{
"name": "My First Extension",
"version": "1.0",
"description": "The first extension that I made.",
"manifest_version": 2,
"browser_action": {
"default_icon": "icon_128.png"
},
"background": {
"scripts": ["background.js"]
}
}
Move background.html to background.js, and remove the <script></script> tags.
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.create({'url': "http://calpinemate.com"});
});
Maybe it is because you are missing theclosibg }) in your background page.
Besides, you don't need any permissions for what you want and it is recommended to use anon-persistent background page (a.k.a. event page).
E.g.:
background.js:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.create({ "url": "http://calpinemate.com" });
});
manifest.json:
{
"manifest_version": 2,
"name": "Test Extension",
"description": "...",
"version": "0.0",
"background": {
"persistent": false,
"scripts": ["background.js"]
},
"browser_action": {
"default_title": "Test Extension"
"default_icon": "icon_128.png"
}
}
BTW, since you seem to be interested in changing the extension icon, you can take a look into chrome.browserAction.setIcon().
Below is an example that toggles the extension icon when the user clicks on the browser-action (you can adapt it according to your specific needs).
background.js:
var icons = ["red.png", "green.png"];
chrome.browserAction.onClicked.addListener(function(tab) {
var iconIdx = localStorage.getItem("iconIdx");
var newIdx = iconIdx ? (parseInt(iconIdx) + 1) % 2 : 1;
chrome.browserAction.setIcon({ path: icons[newIdx] });
localStorage.setItem("iconIdx", newIdx);
});

ExecuteScript method

Basically I'm trying to do a little chrome extension following Google documentation. I'd like to inject a script every time the extension button is clicked.
This is my manifest so far:
{
"name": "Example",
"manifest_version": 2,
"version": "1.0",
"permissions": [
"tabs"
],
"description": "My Chrome extension.",
"browser_action": {
"default_icon": "icon.png"
},
"background": {
"scripts": ["background.js"]
}
}
And this is my background.js:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(tab.id, {code: "content_script.js"});
});
The problem is that the content_script is not fired, even trying with such a simple alert("aaa");
Can you please tell me what I'm doing wrong? I can't figure it out.
In order to execute a content script on a page, you must request the correct host permissions in your manifest file.
Since you want to insert a content script on click of a browser action button, it suffices to request the activeTab permission. Furthermore, you can drop the tabs permission, to reduce the number of permission warnings to zero!
{
"name": "Example",
"manifest_version": 2,
"version": "1.0",
"permissions": [
"activeTab"
],
"browser_action": {
"default_icon": "icon.png"
},
"background": {
"scripts": ["background.js"]
}
}
(The activeTab permission has been introduced in Chrome 26. If you need to support Chrome 25 and earlier, add the *://*/* or <all_urls> permissions to the manifest file).
Note: If you add a callback to chrome.tabs.executeScript, you would have received a useful error message in chrome.runtime.lastError.message:
Cannot access contents of url "http....." Extension manifest must request permission to access this host.
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(tab.id, {
file: "content_script.js"
}, function() {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
}
});
});
In addition to Rob's fix you should be using {file: "content_script.js"}

chrome content script onclick event

I'm trying to write a chrome extension and cannot seem to understand how to implement the following scenario:
user is on page X
user clicks on the extension's button
something happens (specifically, user is redirected to some url)
here's the manifest.json:
{
"name": "My First Extension",
"version": "1.0",
"description": "The first extension that I made.",
"browser_action": {
"default_icon": "icon.png",
"default_title": "my title"
},
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["myscript.js"]
}
],
"permissions": [
"tabs", "https://*/*"
]
}
and here's myscript.js:
alert('entered myscript.js..');
function doMagic()
{
alert('extension button clicked!!');
}
chrome.extension.onClicked.addListener(doMagic);
i know im missing something really obvious, but cant seem to figure it out from the docs, other sites, etc.!!
Don't use a content_script, you really only need those if have to have access to the HTML of the tab.
Use a background_page for the onClicked listener and chrome.tabs.update to redirect the page.
function doMagic(tab) {
chrome.tabs.update(tab.id, { url: 'http://www.google.com' });
}
chrome.browserAction.onClicked.addListener(doMagic);

Resources