gpt4 book ai didi

javascript - 用于突出显示和提醒网页上所有链接的 Google Chrome 扩展程序

转载 作者:行者123 更新时间:2023-11-30 10:36:41 26 4
gpt4 key购买 nike

我是编写 Google Chrome 扩展程序的新手。我(特别地)打算制作一个 browser action单击该扩展程序将打开我们 Google Search 时产生的页面上的所有链接(URL)任何主题。到现在为止我可以做一些 highlights alerts (javascript alert function )任何网页上的链接。
manifest.jsonscript所需文件如下:-


<强> manifest.json==>

{
"name": "Highlight then alert.",
"description": "Highlight and alert links on a webpage.",
"version": "1.1",
"background": {
"scripts": ["write.js"]
},
"permissions": [
"tabs", "http://*/*", "https://*/*"
],
"browser_action": {
"default_title": "highlighter",
"default_icon": "highlight_16x16.png"
},
"manifest_version": 2
}

write.js==>

chrome.browserAction.onClicked.addListener(function(tab) {

var action_url1 = "javascript:(function()
{for(i=0;i<document.links.length;i++)alert(document.links[i]);})();";

<强> var action_url2 = "javascript:(function()
{for(i=0;i<document.links.length;i++)document.links[i].style.backgroundColor='#F00';})();";


chrome.tabs.update(tab.id, {url: action_url1});
chrome.tabs.update(tab.id, {url: action_url2});
});


现在,我正在写一个简单的 inline javascript 代码,如图所示将它们存储在不同的变量中,然后将它们传递给 Chrome 对象方法
- chrome.tabs.update() 执行 highlightalert操作。
我需要找出一种方法来通过编写绝对 javascript 函数而不是传递变量来执行相同的操作。
有什么建议吗?

最佳答案

将突出显示 Google 搜索扩展中的所有链接的示例骨架,您可以根据需要对其进行自定义。如果您需要更多信息,请告诉我

enter image description here

manifest.json

{
"name": "Show Links",
"description": "Show links in a page",
"version": "0.1",
"minimum_chrome_version": "16.0.884",
"permissions": [
"experimental", "tabs", "downloads","<all_urls>"
],
"browser_action": {
"default_icon": "icon.jpg",
"default_popup": "popup.html"
},
"manifest_version": 2
}

popup.html

<html>
<head>
<script src='popup.js'></script>
</head>
<body>
<table id='links'>
<tr>
<th><input type='checkbox' checked id='toggle_all'></th>
</tr>
</table>
</body>
</html>

popup.js

var allLinks = [];
var visibleLinks = [];

// Display all visible links.
function showLinks() {
var linksTable = document.getElementById('links');
while (linksTable.children.length > 1) {
linksTable.removeChild(linksTable.children[linksTable.children.length - 1])
}
for (var i = 0; i < visibleLinks.length; ++i) {
var row = document.createElement('tr');
var col0 = document.createElement('td');
var col1 = document.createElement('td');
var checkbox = document.createElement('input');
checkbox.checked = true;
checkbox.type = 'checkbox';
checkbox.id = 'check' + i;
col0.appendChild(checkbox);
col1.innerText = visibleLinks[i];
col1.style.whiteSpace = 'nowrap';
col1.onclick = function() {
checkbox.checked = !checkbox.checked;
}
row.appendChild(col0);
row.appendChild(col1);
linksTable.appendChild(row);
}
}



// Add links to allLinks and visibleLinks, sort and show them. send_links.js is
// injected into all frames of the active tab, so this listener may be called
// multiple times.
chrome.extension.onMessage.addListener(function(links) {
for (var index in links) {
allLinks.push(links[index]);
}
allLinks.sort();
visibleLinks = allLinks;
//console.log(links);
showLinks();
});

// Set up event handlers and inject send_links.js into all frames in the active
// tab.
window.onload = function() {


chrome.windows.getCurrent(function (currentWindow) {
chrome.tabs.query({active: true, windowId: currentWindow.id},
function(activeTabs) {
chrome.tabs.executeScript(
activeTabs[0].id, {file: 'send_links.js', allFrames: true});
});
});
};

send_links.js

// Send back to the popup a sorted deduped list of valid link URLs on this page.
// The popup injects this script into all frames in the active tab.
console.log("Injected");

var links = [].slice.apply(document.getElementsByTagName('a'));
console.log(links);
links = links.map(function(element) {
// Return an anchor's href attribute, stripping any URL fragment (hash '#').
// If the html specifies a relative path, chrome converts it to an absolute
// URL.
var href = element.href;
var hashIndex = href.indexOf('#');
if (hashIndex >= 0) {
href = href.substr(0, hashIndex);
}
return href;
});

links.sort();

// Remove duplicates and invalid URLs.
var kBadPrefix = 'javascript';
for (var i = 0; i < links.length;) {

if ((((i > 0) && (links[i] == links[i - 1])) ||
(links[i] == '') ||
(kBadPrefix == links[i].toLowerCase().substr(0, kBadPrefix.length))) ) {
links.splice(i, 1);
} else {
++i;
}
}
console.log(links);

chrome.extension.sendMessage(links);

关于javascript - 用于突出显示和提醒网页上所有链接的 Google Chrome 扩展程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13626218/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com