How to get the active tab window function? - javascript

I'm developing a google chrome extension. I need to get the active tab window function. and update the window function. But can't get the active tab window function.
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id! },
function: () => {
console.log(window);
}
})
});
Manifest.json
{
"name": "Dummy Extension",
"version": "1.0",
"description": "Build an Extension with Angular",
"manifest_version": 3,
"permissions": [
"tabs",
"activeTab",
"scripting"
],
"background": {
"service_worker": "background.js"
},
"action": {
"default_title": "Chrome extension",
"default_popup": "index.html"
},
"host_permissions": [
"http://localhost:8004/*",
"https://localhost:8004/*"
]
}

Related

Chrome extension fetching website id data in window

On click inside the extension
Fetch id"dfa5" text data and alert
I am looking for when someone clicks inside the extension button, then fetch h1 id"" data. and alert
manifest.json
{
"manifest_version": 2,
"name": "Sample Name",
"version": "0.1.0",
"description": "This is a sample description",
"short_name": "Short Sample Name",
"permissions": ["activeTab","tabs", "declarativeContent", "storage", "<all_urls>"],
"content_scripts": [
{
"matches": ["<all_urls>"],
"css": ["background.css"],
"js": ["background.js"]
}
],
"browser_action": {
"default_title": "Does a thing when you do a thing",
"default_popup": "popup.html",
"default_icon": {
"16": "icons/icon16.png",
"32": "icons/icon32.png"
}
}
}
background.js
document.addEventListener("DOMContentLoaded", () => {
var button = document.getElementById("asdfjk")
button.addEventListener("click", () => {
sdef();
})
})
function sdef() {
var ksdfh = document.getElementById("dfa5").innerHTML; // current website page h1 id="dfa5"
alert(ksdfh)
}

How do I make a button that executes the background.js in a chrome extension?

I am making a chrome extension. Here is the code:
Manifest.json:
{
"name": "My name!",
"version": "2.0",
"description": "Blocks ads",
"permissions": ["webRequest", "webRequestBlocking", "<all_urls>"],
"browser_action": {
"default_icon": "shield.ico",
"default_popup": "popup.html",
"default_title": "Ad be gone"
},
"background": {
"scripts": ["background.js"]
},
"manifest_version": 2
}
background.js:
chrome.webRequest.onBeforeRequest.addListener(
function(details) { return {cancel: true}; },
{ urls: ["*://*.doubleclick.net/*", "*://*.ads.google.com/*",] },
["blocking"]
);
How would I make a button in popup.html that turns on and off the background.js and alerts the user that it is doing so? I basically want to make a button that turns on and off the ad blocker.

How to capture/intercept web page's console log in a my chrome extension?

I know there are 3 different contexts : background, popup, web page. I want to capture/intercept a web page's console log in my extension, How can I do that?
Here is my code:
contentScript.js:
console.log('starting>>>>');
var log = console.log;
console.log = function () {
log.call(this, '**** MY Console**** ');
log.apply(this, Array.prototype.slice.call(arguments));
// ...
}
};
manifest.json:
{
"name": "my extension 01",
"version": "1.0",
"description": "----",
"permissions": ["activeTab","storage","declarativeContent"],
"background":{
"scripts":["background.js"],
"persistent":false
},
"page_action": {
"default_popup": "popup.html"
},
"options_page": "options.html",
"content_scripts": [
{
"matches": ["https://MY-WEBSITE/*"],
"run_at":"document_start",
"js": ["contentScript.js"]
}
],
"manifest_version": 2
}

How to Append Content to Current Page (Active Tab) DOM Using Chrome Extention

I am trying to add some content to active document(Active tab) by Chrome extension. Here is what I have:
popup.html
<button class="btn btn-default" id="clear" type="submit"></button>
popup.js
$('#clear').on('click', function () {
$('tr').eq(0).append("Some Test");
});
and manifest.json
{
"manifest_version": 2,
"name": "Adder",
"description": "Adding Content",
"version": "1.0",
"background" : {
"scripts" : ["jquery.min.js","popup.js"],
"persistent": false
},
"content_scripts": [ {
"js": [ "jquery.min.js", "popup.js"],
"matches": [ "http://*/*", "https://*/*"]
}],
"browser_action": {
"default_icon": "icon.png"
},
"permissions": [
"activeTab"
]
}
Update
$('#clear').on('click', function () {
chrome.tabs.executeScript({ code:
'document.body.style.backgroundColor="red"' });
alert("test");
});

Google Chrome notifications don't show

I want send notification by Google Chrome Extension
Manifest
{
"name": "Test",
"description": "Test",
"version": "1.1",
"background": {
"scripts": ["background.js"]
},
"permissions": [
"tabs", "http://*/*", "https://*/*",
"notifications"
],
"browser_action": {
"default_title": "Test",
"default_icon": "test_16x16.png"
},
"manifest_version": 2
}
background.js
chrome.browserAction.onClicked.addListener(function() {
chrome.notifications.create(
'name-for-notification',{
type: 'basic',
iconUrl: 'image.jpeg',
title: "This is a notification",
message: "hello there!"
},
function() {}
);
});]
Why doesn't it work?

Categories