Chrome extension: Copy text - google-chrome-extension

I have a simple chrome extension where I'd like to click an image and copy the href of the link next to it. I have everything in place, but the copy will not work for the life of me. I have the following premissions in my manifest:
"permissions": [
"clipboardRead",
"clipboardWrite"
]
I then create an input with id "copyInp" and use the following function to copy to clipboard (tried to be as deliberate as possible here, so it's not the most compact):
function copyLinkToClipboard( link ) {
$("#copyInp").val(link);
var inp = document.getElementById("copyInp");
inp.focus();
inp.select();
document.execCommand('copy');
};
After this runs, I get nothing new in my clipboard when I ctrl+v. Any ideas as to what's going wrong here? The input definitely has the text I want in it, and I have the permission in the manifest. Any help would be greatly appreciated...

After a bit more digging I discovered I needed to use a background page. Apparently this is the only thing you can call document.execCommand on. So the fix is to create a background.html with the copy function and the input in it, add a listener like so:
chrome.extension.onRequest.addListener(function(obj) {
copyLinkToClipboard( obj.link );
});
and then use sendRequest to pass the text you want copied to the background page:
chrome.extension.sendRequest({link: linkText});
and don't forget to add the background page in the manifest
"background_page": "background.html",
voila. text copied to clipboard. would LOVE an easier way to do this (if security is the issue then why not make an api for extensions? or rather, why scrap the experimental api only to force us to do this stupid workaround?) but oh well, this will suffice for the time being

Related

How to redirect to a website in Godot Web

I'm planning on trying to see if it's possible to make a website in Godot(Yes, I know I shouldn't I just want to try just to try). I thinking about and looking over the features I need and I have one problem.
I just need a way for a person to press a button and get redirected to my itch games. I don't care if it creates a new tab or changes the current tab. Thank you for any help.
If you dont export to web you can call
OS.shell_open("url")
Sadly this does not work in an html export. A solution I found for myself is the JavaScript Interface. As the name suggested it allows you to execute Javascript.
So to open a URL you could connect the pressed signal of a button to something like this:
if OS.has_feature('JavaScript'):
JavaScript.eval("""
window.open('https://google.com', '_blank').focus();
""")
This will open a new tab in the active browser.
I also found an article on the godot site, basically asking the same question (https://godotengine.org/qa/46978/how-do-i-open-richtextlabel-bbcode-links-in-an-html5-export). Here they tried to use an RichTextLabel with BBCode.
The solution did not work for me, when I tested it, though.
As pointed in the comments you can try OS.shell_open, for example:
OS.shell_open("https://example.com")
That only works if it is not an HTML export.
Your other alternative is to eval JavaScript, for example this navigates the current tab:
JavaScript.eval("window.location.href='https://example.com'")
Which only works if it is an HTML export.
Since that only works for an HTML export and the other does not work on an HTML export... If you need both you can do this:
if OS.get_name() == "HTML5":
JavaScript.eval("window.location.href='https://example.com'")
else:
OS.shell_open("https://example.com")
See also Close a game in Godot.

Get Tabs URL, copy to new tab, change something in domain

Basically I just want to check links across different environments and thats just a different domain name. So basically I have my extension so far with buttons for each one but am having trouble getting anything to happen. Tried in b ackground.js as I thought that was the only place to access tab?
Tried this and some other variations but yeah this is very new to me so forgive my ignorance. I know javascript well enough but this is taking a bit to adjust.
let dev = document.getElementByID("dev");
console.log(dev);
dev.onclick = function(element) {
chrome.tabs.query({currentWindow: true, active: true}, function (tab) {
chrome.tabs.update(tab.id, {url: "www.google.com"});
});
1) An URL must start with a scheme: https://www.google.com. 2) Make sure to read about the extensions overview::architecture - the popup is a separate page with its own window, URL, and devtools which you can access by rightclicking the popup, then clicking Inspect. 3) Hint: no need to use tabs.query to change the active tab - simply omit tab.id, from tabs.update as it's an optional parameter. – wOxxOm
Thanks that helped a lot!

Code not executing?

I have a simple extension for Google Chrome that should collect data from you with a variable and prompt, and then should print what you inputted with document.write(variable_name);
Upon entering the code into the prompt, it does not print it.
I know the code is right, it works if I paste it into console, just not as an extension
How do I fix this?
var variable_name = prompt("Enter something");
document.write(variable_name);
You haven't provided anywhere near enough context, but I'm going to toss out a theory here:
You're probably running that code in your extension's background page. If that's the case, you'd have an entry something like this in your manifest.json:
"background": {
"scripts": ["background.js"]
}
Background code is executed on its own thread, with its own DOM. It can not interact with the current page. What you need is a content script. You'll make an entry in manifest.json that describes what pages you want it to be injected into. If you'd like to inject your code into every page, you can use "matches": ["<all_urls>"]. Otherwise, see this page for help with creating a pattern (of set of patterns) to match the page(s) you want.
...or maybe I'm completely wrong, and you're already using a content script. If that's the case, please provide your entire manifest.json and any other code that's used in your extension.

ExecuteScript in a random tab that is not part of the chrome extension send result back to background javascript

I would like to check the html of an element of a
chrome.tabs.executeScript(tab_id,{code: 'sendRequestToBackground(document.getElementById('important_val').innerHTML); '},
function(){
});
I want to be able to get this value from my background script, I saw a lot of examples with chrome.extension.sendRequest and chrome.extension.onRequest.addListener because all the examples is for working from the extension popup.html to background script and vice-versa.
But I open a brand new tab, change the URL, and I want to get a value of a field that (which btw is generated via Javascript) belongs to this random tab.
Is it possible to do that?
Thanks!
If you want to inject your code to a random tab, do this:
Be sure to have permissions to "tabs" and "<all_urls>" in your manifest.json
Get all tabs using chrome.tabs.query
Pick random tab using Math.random()
Inject the code using chrome.tabs.executeScript
In the incjected code call chrome.extension.sendRequest
On the background page receive the message using chrome.extension.onRequest

How to play audio from a Google Chrome extension even when closed?

I've been making a Google Chrome extension, one that streams an audio file from another site. I use a Google audio player to do this, and it works fine.
The only problem is, the soundtrack stops playing when I leave the extension is closed. Right now, I don't use Java and would prefer to keep it that way, since I have about zero experience in that field. If I need to, I am perfectly willing to use it.
<embed type="application/x-shockwave-flash" flashvars="audioUrl=AUDIO URL HERE" src="http://www.google.com/reader/ui/3523697345-audio-player.swf" width="400" height="27" quality="best"></embed>
This is what I'm using to play my audio files. I need to somehow get this to keep playing in the background page. I've tried making the main background page and the popup page the same; doesn't work. Here's my manifest file, with the popup and background pages one and the same:
"background_page": "Main Code.html",
"permissions": [
"background"
],
"icons" : {
"16" : "KH icon_16.png",
"48" : "KH icon_48.png",
"128": "KH icon_128.png"},
"browser_action" : {
"default_icon" : "KH icon_19.png",
"default_popup": "Main Code.html"
Please help me, I've scoured the web for info on this, and being an amateur programmer, I really need help.
From what I seem to understand. Your problem is that as soon as you leave the extension the audio stops playing. I would suggest that you instead of using background_page, you should add a background script.
Use Manifest version 2.
Add a background script like this.
"background":
{
"scripts": ["background.js"]
}
play your audio files through this background script.
I think this will solve your problem.
Um... I think you mean Javascript and not Java (2 completely different languages - not related in any way what so ever). Next you should get rid of all the spaces in your file names, not only is this terrible practice but your manifest is probably having trouble identify all of the the extensions components.
Finally... It may work to play the audio from the background page. Refer to the Google Chrome Extensions API for what a background page is. The code on the popup is not going to be the same as the code in the background page. To talk between the two refer to the chrome.extension.* package API. For a quick solution here is a socket utility that provides two way communication.

Resources