I am creating a chrome extension which extracts the inner text of h2> from the page in which the user is browsing. Although I learned about content scripts and have used it but it is not working I don't know where is the problem. If anyone could help me that would be greatly appreciated.
This is the js code:
document.addEventListener('DOMContentLoaded', function() {
var link = document.getElementById('button');
link.addEventListener('click', function() {
updateresult();
});
});
function updateresult() {
var i;
var x =document.getElementsByTagName("h2");
for(i=0; i< x.length ; i++)
{
document.getElementById("result").innerHTML = (x[i].innerHTML + "\n");
}}
This is the manifest file which I am using:
{
"manifest_version": 2,
"name": "Header Extractor",
"description": "Extension to search header",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"16": "icon.png",
"48": "icon.png",
"128": "icon.png",
"default_popup":"popup.html",
"default_title": "Open And Search headers"
},
"permissions": [
"storage",
"activeTab" ,"tabs"
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": [ "popup.js"],
"run_at": "document_end"
}
]
}
Have you already created a result div to paste your results in?
Also your current function only returns one h2 value. Let's use concat to output them all together.
function updateresult() {
var i;
var x = document.getElementsByTagName("h2");
var string = "";
for(i=0; i<x.length; i++)
{
string.concat(x[i].innerText + "\n");
}
document.getElementById("result").innerText = string;
}
As a side note, we use innerText instead.
Related
My Work So FAR
Manifest.json
{
"manifest_version": 2,
"name": "Extension For PyIDM",
"version": "0.1",
"description": " An Open Source Alternative Of IDM",
"icons": {
"16": "icons/favicon-16x16.png",
"48": "icons/android-icon-48x48.png",
"120": "icons/apple-icon-120x120.png"
},
"author" : ["Harshil Pandey","#PyIDM"],
"permissions": [
"<all_urls>",
"downloads"
],
"background": {
"scripts": ["background.js","pyidm_extension.js","webextension.js"]
}}
Background.js
chrome.runtime.onInstalled.addListener(function() {
//window.open("install.html")
window.open('install.html','height=480,width=640', false);
});
chrome.downloads.onCreated.addListener(function() {
var r = confirm("Do you Want To Download"+ File + " Using PyIDM");
if (r == true) {
var final = chrome.downloads.onCreated.finalUrl;
alert(final + " I Am working");
//alert("You pressed OK!");
} else {
//alert("You pressed Cancel!");
}
}
)
Problem
How to Get The Url OF The File That IS Going Downloads
And The File NAme
What i am using to know if a file is going to download: chrome.downloads.onCreated
I have made a chrome extension to run on GMail, in which I would like to trigger a Google app Script with a button I made. Can anyone tell me how to trigger/call Google script.
This is my script:
function myFunction() {
var thread = GmailApp.getInboxThreads(0,1)[0];
var message = thread.getMessages()[0];
var sub = message.getSubject();
var body = message.getPlainBody();
var text = /(Event)|(Party)/;
result = sub.match(text);
if(result == null) {
result = body.match(text);
}
var q=0;
var labels = GmailApp.getUserLabels();
for (var i = 0; i < labels.length; i++) {
if(labels[i] === "Events") {
break;
}
else
q++;
}
if(q==labels.length)
GmailApp.createLabel("Event")
if(result != null) {
label = GmailApp.getUserLabelByName("Event");
label.addToThread(thread);
}
GMailApp.createLabel("Event");
}
My manifest.json file is below.
{
"manifest_version": 2,
"name": "drc",
"version": "0.1",
"background": {
"scripts": ["bg.js"]
},
"content_scripts": [
{
"matches": [
"https://mail.google.com/*",
"http://mail.google.com/*"
],
"js": ["content.js", "onclic.js"],
"css": ["drpdwn.css"]
}
],
"browser_action": {
"default_title": "Append Test Text"
},
"web_accessible_resources": ["logo.png", "jquery-1.10.2.min.js"],
"permissions": [
"tabs",
"activeTab",
"https://mail.google.com/*",
"http://mail.google.com/* ",
"https://www.googleapis.com/auth/gmail.*"
]
}
Now whenever a user install my extention, google script function should also be installed automatically, How do I do that.
Use the doPost() or doGet() trigger and execute your Google Apps Script.
My aim is to search for a div by id if it exists in all languages my site supports.
manifest.json
{
"manifest_version": 2,
"name": "Hello World!",
"description": "This extension is to check if link is not visible when it should not :)",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"content_scripts": [ {
"js": [ "jquery-1.11.1.min.js"],
"matches": [ "http://*/*", "https://*/*"]
}],
"permissions": [
"tabs",
"http://*/*",
"https://*/*"
]
}
popup.html -
<html>
<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<style>
table{background:#CCC;border:1px solid #000;}
table td{padding:15px;border:1px solid #DDD;}
</style>
<body>
<input type="hidden" id="currentLink"/>
<hr />
Checking ...
<div id="dynamictable"></div>
<script type="text/javascript" src="currentUrl.js"></script>
</body>
</html>
currentUrl.js -
chrome.tabs.getSelected(null, function(tab) {
var currentTab = (tab.url);
var languages = [ "en", "fr" ];
$('#dynamictable').append('<table></table>');
var table = $('#dynamictable').children();
var i;
for (i = 0; i < locales.length; ++i) {
chrome.tabs.update(tab.id, {active:true, url: currentTab + "?languageParam=" + languages[i]});
var query = document.querySelector("div#header");
if (query) {
alert("This page has a friendly div");
}
table.append("<tr><td>" + languages[i] + "</td><td>" + "</td></tr>");
}
});
In above query is not coming. How can I parse the DOM after chrome.tabs.update ?
Have you tried using a callback?
The api defines the function as chrome.tabs.update(integer tabId, object updateProperties, function callback).
Perhaps you could change
chrome.tabs.update(tab.id, {active:true, url: currentTab + "?languageParam=" + languages[i]});
for
chrome.tabs.update(
tab.id,
{active:true, url: currentTab + "?languageParam=" + languages[i]},
function() { // this is the callback you may want to use.
var query = document.querySelector("div#header");
if (query) {
alert("This page has a friendly div");
}
}
);
This may not work as-is, but I hope the idea is clear.
I have an extension to count the number of stylesheets.
When the icon is pressed, the stylesheets of all opened windows or tabs are shown.
However, the extension has no effect on the windows and tabs that are opened later.
How can I solve it ?
manifest.json
{
"name": "StyleSheets counter",
"description": "It counts the stylesheets linked with the web page",
"version": "2.0",
"permissions": [
"tabs",
"*://*/*",
"activeTab",
"debugger"
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"browser_action": {
"default_icon": "SS Counter"
},
"icons": {
"48": "on.png",
"48": "off.png"
},
"manifest_version": 2
}
background.js
chrome.browserAction.onClicked.addListener(function(tab) {
executeScriptsInExistingTabs();
});
function executeScriptsInExistingTabs(){
chrome.windows.getAll(null, function(wins) {
for (var j = 0; j < wins.length; ++j) {
chrome.tabs.getAllInWindow(wins[j].id, function(tabs) {
for (var i = 0; i < tabs.length; ++i) {
if (tabs[i].url.indexOf("chrome://") != 0) {
chrome.tabs.executeScript(tabs[i].id, { file: 'counter_ss.js' });
}
}
});
}
});
}
counter_ss.js
var theSheets = document.styleSheets;
alert("Number of stylesheets: " + theSheets.length);
Add onClicked event listener with your function executeScriptsInExistingTabs as callback.
I have already the solution, for the new tabs and for the ones whose URL is changed:
chrome.tabs.onCreated.addListener(function(tab) {
//none is done here
});
chrome.tabs.onUpdated.addListener(function(tabid, info, tab) {
if (info.status == "complete") {
chrome.tabs.executeScript(tabid, {file:'counter_ss.js'});
}
});
The code inside
chrome.tabs.onUpdated.addListener
is run both, when the new tabs are open and when the URL of a tab is changed. So to avoid duplication, none is done in the
chrome.tabs.onCreated.addListener
However, it must be there (altough empty).
I am having problems with my google extension, I have followed what I believe is proper for sending information to a background page, but when I try to run my extension I am hit with "Uncaught TypeError: Object [object DOMWindow] has no method 'closer'" Does anyone know what is happening and why?
popup.html
var i = 0;
function start(){
var bg = chrome.extension.getBackgroundPage();
bg.closer(i); //chrome.extension.sendRequest({});
}
function add(){
i++;
document.getElementById('box').value=i;
}
function sub(){
i--;
document.getElementById('box').value=i;
}
background.html
var ctr = 0;
function closer(int i){
var t=setTimeout("close()",i*500);
}
function close(){
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.remove(tab.id, function() { });
});
}
manifest.json
{
"name": "Hello World!",
"version": "1.0",
"description": "My first Chrome extension.",
"permissions": ["tabs", "background"],
"background_page": "background.html",
"browser_action": {
"default_icon": "icon.png",
"popup": "popup.html"
}
}
you cannot just send message to that bg variable.
There is a special approach to send messages to bg, please read up on sendRequest() etc.
http://code.google.com/chrome/extensions/extension.html#method-sendRequest