Detect empty, new tab openings in Google Chrome - url

I just published a Google Chrome extension which loads background images into new, empty tabs. The plugin is found here in the Chrome Web Store.
For detecting new and empty tabs, I need to ask for "tab" permissions in the extension's manifest.json. This, however, gives the extension the permission to read the browser's history. Not all users will want this and we don't actually need it. Is there a way to detect empty tabs without this permission requirement? Currently our check looks like this:
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){
if (changeInfo.status == "loading" && tab.url == 'chrome://newtab/')
{ /* load background into tab */ }
});

If you want to customize the new tab page you should add this to your manifest:
"chrome_url_overrides": {
"newtab": "newtab.html"
}
Then place the script you are currently injecting into new tab pages as a <script> tag in the newtab.html file. That won't cause that permission message.

Related

Ability to prevent/stop loading 'Page load' in Cypress when clicked a hyperlink

In my Cypress test,
I need to test a link which downloads a .txt, .xlsx and a .zip file when clicked, but when I use "click()" to click the hyperlink, it starts a page load and expects a new action to happen as a result of clicking a link.
As an alternative to this I tried using the cy.downloadFile() to download the files directly through the link but the link I am using is dynamically generated hence I am unable to use that as well. Hence I want to store the newly generated link in the variable and then use it in cy.downloadFile() every time I run the test.
Are there any other ways to test a hyperlink or how to store the dynamically generated link every time the test is run?
Hi there i been through this problem before.
I think it is the same scenario where the cypress test runner just keep waiting page load event and not go to the next test command while file are successfully downloaded.
Add custom cy.window event to listen the click event and add timeout to "force" reload current page, you need to tweak the timeout value so it suitable for your test.
it.only('tes button download', ()=> {
// visit url
cy.visit("/spmb/list_nilaiseleksi");
cy.get('#wrap-button > .btn').click()
//download
cy.window().document().then(function (doc) {
doc.addEventListener('click', () => {
setTimeout(function () { doc.location.reload() }, 5000)
})
cy.get(':nth-child(9) > .col-md-12 > .btn').click()
})
})
More details and discussion available here on cypress github issue :
https://github.com/cypress-io/cypress/issues/14857
I found a way to store the link of the element I want to click. Since I am able to store the link, I am able to solve the requirement by using cy.downloadFile().
cy.get('#selector', {timeout: 15000}).then(($input) => {
cy.downloadFile($input.attr('href'),'cypress/downloads','filename.csv')
});
For me, the problem was resolved after I added the download attribute to the link, This is changing the element in the DOM, but it keeps the solution simple:
cy.get('[href="download?type=php"]')
.then((el) => {
el.attr('download', '');
})
.click();

Thunderbird 67 Extension doesn't inject Content Script in the main tab

I'm making a Thunderbird Extension through the WebExtension Api (with popup) and i've some difficulties running a Content script to get some elements (attachments list of the current mail for example) directly from Thunderbird interface.
I've made a popup.html , a popup.js and a contentScript.js that runs when the popup is opened. Apparently contentScript.js doesn't run in the main Thunderbird tab, but runs smoothly when i open a new tab (with url) through code.
$(document).ready(function(){
console.log('Try to execute contentScript');
// -- create new tab --
// browser.tabs.create({
// url: 'https://example.org'
// });
// -- execute script in current tab --
browser.tabs.executeScript({
file: 'scripts/contentScript.js'
});
});
// from contentScript.js
console.log('contentScript.js - Injected');
In console i expected "contentScript.js - Injected" but this only appens when i am not in the main Thunderbird tab.
When i am in the main tab it shows only "Try to execute contentScript" then nothing appens, no errors.
Thunderbird extension are evolving right now from the old legacy method (overlay of xul files) to the actual WebExtension Api used by most of the browsers, but there are some differences: in Firefox the extension works without any problem, so i supposed that the main tab of Thunderbird is somehow protected from content injection.
My objective here is to get the attachments list and other elements directly from the interface, but apparently i can't.

How Automatically On "Content Blocker" Extension in Safari section?

I am creating an Ad Blocker. I am just trying to Automatically on safari extension of "Content Blocker". I went through examples but did't found any solution. Is there any way to "ON" extension or manually we have to start it?
On iOS, Safari Content Blockers are disabled by default.
There is no way to automatically enable them from your app. You must instruct the user to:
Open the Settings app.
Go to Safari > Content Blockers.
Toggle on your Content Blocker extension.
On macOS (as of 10.12), a similar rule applies: Content Blocker extensions (bundled with your app) are disabled by default, and must be toggled on by the user in Safari Preferences > Extensions.
Assuming you want to test your "personal AdBlock program", first prepare a dummy HTML, with this line <div class="ads">hello</div>,
next apply your "personal AdBlock program", assuming it is JavaScript/CSS based and not proxy-like, you either hide, or remove the element (Node) from the DOM.
for example:
document.querySelector('div[class*="ads"]') -- this is nice and (very) generic way to find the element.
this is how to hide "the ads"
document.querySelector('div[class*="ads"]').style.display="none";
or, to make it stronger, related to other rules on the page, make it a local style + important notifier: document.querySelector('div[class*="ads"]').style.cssText="display:none !important;" ;
you can also remove the element (Node) from the DOM:
var e = document.querySelector('div[class*="ads"]') follow by:
e.parentNode.removeChild(e);
now, you probably want to see that "YOUR ADBLOCK" worked,
later (after the page has loaded, and your javascript code runned) type:
console.log(null === document.querySelector('div[class*="ads"]') ? "removed(success)" : "still here(failed)")
note that for this example (to make things simple) I assume there is only one div with that class in the page (avoiding loops :) ).
if you've just going to hide the element, you should query its current (most updated) style-condition, using a native method exist under window:
console.log("none" === window.getComputedStyle(document.querySelector('div[class*="ads"]')) ? "hidden(success)" : "still here(failed)")
Enjoy!

Firefox extension convert to restartless with XBL

I'm developing a Firefox extension and need some help with it.
My extension is overlayed and uses XBL binding to add new items to the user interface.
Is it possible to convert this extension to a bootstrap type?
I'm trying to add a button to the findbar.
Used XBL to override the findbar interface.
To start with the bootstrap I included "findbar{ -moz-binding:... }" rule to the style.css and register this sheet on startup()
(with nsIStyleSheetService.loadAndRegisterSheet()).
In this case my button is added to the bar without restart.
But when I disable or remove the addon I need to restart the browser so that the button disappear.
I tried to remove it on shutdown() in the bootstrap.js using:
var fb=window.gFindBar.getElement("findbar-container")
var but=window.gFindBar.getElement("the-button")
fb.removeChild(but)
But this didn't remove it. I debugged the code and all the elements (fb, but) were found and removed but it didn't touch the real findbars in any tab I had or opened.
So I tried to unregister the stylesheet which bind my XBL to the findbar. This time the findbar just didn't open in the current tabs.
But in new tabs it opened and without the button (a little better...).
Also I've found that the findbar didn't open in the opened tabs because of a strange error:
"this.browser is undefined".
This error pointed to the findbar.xml line 533 where the code tried to run _unpdateCaseSensitivity() but it couldn't get the "gFindBar._browser" prorperty.
Maybe it's because this property wasn't loaded for the original findbar object from the browser start (it was used by the overriden findbar object)...
So this is the point I stuck on...
And the question now is:
How can I delete the button without restart and so that the findbar opens?
From the Findbar Tweak addon I extracted this method to add new checkbox to the findbar (and changed it for my needs):
var findbar=window.gFindBar
var container = findbar.getElement("findbar-container")
var button = $('<toolbarbutton>')
button.setAttribute('anonid', 'test-find-tabs')
button.setAttribute('label', 'Test')
button.setAttribute('type', 'checkbox')
button.addEventListener('command', test,false)
container.insertBefore(button, findbar.getElement('find-case-sensitive').nextSibling)
But for my final purpose it needs some additional editings. So for now I'll leave this problem. Maybe a wise thought will occur me later. Cause a good idea often comes in mind after we change the focus from the problem and then return to it...

How to create Firefox Extension to send active tab URL to its Native, similar to chrome native messaging and install it through msi

I have already developed a C# win form application and a chrome extension with native messaging (another C# console app) to fetch user's active tab url. I have also developed an msi setup in WiX to write under the registry (HKLM/SOFTWARE/Wow6432Node/Google/Chrome/Extensions and HKLM\SOFTWARE\Wow6432Node\Google\Chrome\NativeMessagingHosts) programmatically and tested the installation of the chrome extension by running the setup on different Windows 7 machines. I am publishing this extension on chrome web store and then I shall install the extension on several client computers.
I want to accomplish the same with Mozilla Firefox. I am newbie in this field and going through some Firefox developers guides (XPCOM, js-ctypes etc.) and getting little confused with multiple links.
It would be of great help if anyone can guide me towards the exact solution. Please note that, 1) I have to install the extension programmatically through the same msi package which already contains my own C# app and the chrome extension with native app and 2) the client machines will be Windows only but any version (XP (optional), 7, 8, Server anything).
EDIT:
According to Noitidart's answer, I have made an ffext.xpi from the 4 files (bootstrap.js, myWorker.js, chrome.manifest, install.rdf) and uploaded and installed on firefox, with no error, but nothing happens. At first, I am concentrating on the browser extension part and not the native for now and I just want to have a listener inside the js files such that whenever I switch to a different firefox tab or firefox window, an alert should show up from the browser itself (not from native at this moment) displaying the active tab's URL. I don't even understand how the js files will get called or executed, how this line myWorker.addEventListener('message', handleMessageFromWorker); will work or if I need to add some other listener.
Nothing is happening at all. How to debug or proceed from here? Please help.
To get the current url of the window do this:
aDOMWindow.gBrowser.currentURI.spec
To access all windows do this:
Cu.import('resource://gre/modules/Services.jsm');
let DOMWindows = Services.wm.getEnumerator(null);
while (DOMWindows.hasMoreElements()) {
let aDOMWindow = DOMWindows.getNext();
if (aDOMWindow.gBrowser) {
var currentURL = aDOMWindow.gBrowser.currentURI;
}
}
If you don't check for gBrowser the aDOMWindow.location will be a chrome path, very likely.
So putting this togather with a ChromeWorker (the ChromeWorker will access the js-ctypes) template here: https://github.com/Noitidart/ChromeWorker (this template is the bare minimum needed for communication between ChromeWorker and your js file, in this case bootstrap.js) (bootstrap.js is the js file that is required and is run, the 4 startup,shutdown,install,uninstall functions are required in bootstrap.js)
From bootstrap.js we would do, for now lets do it in startup:
function startup() {
loadAndSetupWorker(); //must do after startup
myWorker.postMessage({
aTopic: 'currentURL',
aURL: Services.wm.getMostRecentWindow('navigator:browser').gBrowser.currentURI.spec // recent window of type navigator:browser always has gBrowser
aLinkedPanel: Services.wm.getMostRecentWindow('navigator:browser').gBrowser.selectedTab.getAttribute('linkedpanel') //we use this to make sure to get the right tab/window on message back
});
}
Then in worker we'll do something like this:
self.onmessage = function(msg) {
switch (msg.data.aTopic) {
case 'currentURL':
var ret = msgBox(0, "alert from ctypes on windows, the url is:" + msg.data.aURL, "ctypes alert windows", MB_OK);
self.postMessage({
aTopic: 'currentURL-reply',
theLinkedPanel: msg.data.aLinkedPanel,
aResponde: aRet
});
break;
default:
throw 'no aTopic on incoming message to ChromeWorker';
}
}
Then back in bootstrap.js we'll receive this message and do something with it:
function handleMessageFromWorker(msg) {
console.log('incoming message from worker, msg:', msg);
switch (msg.data.aTopic) {
case 'currentURL-reply':
let DOMWindows = Services.wm.getEnumerator('navigator:browser');
while (DOMWindows.hasMoreElements()) {
let aDOMWindow = DOMWindows.getNext();
if (aDOMWindow.gBrowser && aDOMWindow.gBrowser.selectedTab.getAttribute('linkedpanel') == msg.data.theLinkedPanel) {
//this is our window, as the currently selected tab linkedpanel is same as one the worker dealt with
var currentURL = aDOMWindow.gBrowser.currentURI;
aDOMWindow.gBrowser.selectedTab.contentWindow.alert('the user was prompted with js-ctypes and the user clicked on:' + aRet);
break;
}
}
break;
default:
console.error('no handle for msg from worker of aTopic:', msg.data.aTopic);
}
}
so what this example does, is on install/startup of addon it gets the most recent browser windows url. it sends it to ctypes, ctypes throws a os level alert, and then ctypes sends back what the user clicked, then bootstrap.js finds that same window and that tab and with a javascript alert it tells what the jsctypes os level msgbox return value was.

Resources